0

Will anyone please tell me what is wrong with this snippet?

<!DOCTYPE html>
<html>
<head>
<script>
function anotherWindow(msg, myWidth, myHeight)
{
 theWindow=window.open('','','width=myWidth,height=myHeight');
 theWindow.document.write(msg);
 theWindow.focus();
}
</script>
</head>
<body>

<input type="button" value="One more window" onclick="anotherWindow('Here is the  window',200,100)" />

</body>
</html>

While the first argument (msg) is successfully passed to method .write, the two arguments related to window size in method .open bring no result —the method sticks to some defaults.

What is wrong with my understanding of variable passing?

Brice Coustillas
  • 1,837
  • 2
  • 11
  • 16
  • 2
    Check this answer: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – elclanrs Mar 13 '13 at 06:01

2 Answers2

2

The arguments are being passed the right way, but they are not being used the right way.

theWindow=window.open('','','width=myWidth,height=myHeight');

You have myWidth and myHeight in quotes which will not tell javascript that those are variables. Those two have to be outside quotes.

Like this:

theWindow=window.open('','','width='+myWidth+',height='+myHeight);
Hanky Panky
  • 44,997
  • 8
  • 67
  • 93
2

You need to actually substitute in the values of the variables.

theWindow=window.open('','','width='+myWidth+',height='+myHeight);
Jeremy Roman
  • 15,584
  • 1
  • 37
  • 42