Notice
Recent Posts
Recent Comments
Link
관리 메뉴

데브마우스

[JavaScript]Uncaught TypeError: window.open is not a function 에러 발생 시 확인해야 할 부분 본문

JavaScript/JS: 에러 정리

[JavaScript]Uncaught TypeError: window.open is not a function 에러 발생 시 확인해야 할 부분

데브마우스 2023. 12. 3. 17:26

자바스크립트에서 Uncaught TypeError: window.open is not a function 에러 발생 시 제일 먼저 확인해야 할 부분이 있습니다.

 

open을 변수명으로 사용하신적이 있나요?

 

자바스크립트는 예약어가 엄격하지 않습니다. 그렇기 때문에 개발자가 open이라는 변수명을 짓고 사용할 경우 window.open의 open 함수를 사용할 수 없게 됩니다.

 

아래는 에러가 발생했던 상황을 재현한 코드입니다.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="test">
    <button id="open">
      버튼입니다.
    </button>
  </div>
  <script>
    var open = document.getElementById("open");
    open.addEventListener("click", oepnFunc);
    function oepnFunc() {
      alert("test");
    }
    var newTab = window.open("devmouse.tistory.com");
  </script>
</body>
</html>