Search code examples
javascripthtmlwindow.open

`window.open` function dosen't open a popup windo but do vanish the existing html and other code


i have a very simple code which tries to open a popup window but it passes to ruin the whole html and other code.

the code is as follows:

<head>
<script type="text/javascript">
    function write(){
/*  var w = String(window.offsetWidth),
        s = String(window.offsetHeight);*/

    var s = window.open('', 'MsgWindow', '_blank');
};
</script>
</head>
<body>
<button onclick="write();" id="writeBtn">Write</button>
</body>

so simple but it dosent do anything!

i don't know what is the problem.

something to note...

  • when the button is clicked then the screen goes white and all the element disappear
  • when i saw this in google console then what i saw was shocking all the html code just disappear
  • i even try to werite in to the variable s like s.document.write('sanmveg') but that dosen't worked

what is the problem?


Solution

  • Rename your function. document.write() is being called instead of your function. Calling document.write() with no parameters causes unexpected behavior like this.

    function mywrite() {
      var s = window.open('', 'MsgWindow');
    };
    <button onclick="mywrite();" id="writeBtn">Write</button>