Javascript window.open size not working properly. Height okay, width messed up

javascriptmethodssizevariableswindow.open

I'm having trouble with some JavaScript and the window.open method.

I'm trying to control the size of the pop up depending on the current time. The hours control the height, and the minutes (rounded to the nearest five) control the width.

The height control is working fine, and corresponds to the correct values… However the width is always at 100% of my screen and I can't work out why.

I have put a test alert to show me the text it should be putting into the window.open method and this looks fine – in fact if I copy and paste the text from the alert into the code and re-load the page, then the height and width both work perfectly. Seems very odd.

Here is my code…

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Untitled</title>
    <meta name="generator" content="BBEdit 10.1" />

<script type="text/javascript">

var d = new Date();
var h = 10*(d.getHours())+300
var m = d.getMinutes();

m = 10*(Math.round(parseInt(m)/5)*5)+300;

var a = "width=";
var b = ", height=";

var size = "'"+a+m+b+h+"'";

var openWin = function(){window.open('','',size)};


function test() {alert(h); alert(m); alert(size);}

</script>
</head>


<body onload="test()">


<input type="button" value="Open Window" onclick="openWin()" />

</body>
</html>

Best Answer

You have quotes around the size.

What you want:

var size = a+m+b+h;
window.open('','',size)

OR

window.open('','', 'width=' + m + ', height=' + h)

What you currently have (after substituting size for what it really is):

window.open('','','"width=' + m + ', height=' + h +'"')

Which evaluates to:

window.open('','','"width=500, height=450"')
Related Topic