Cameron
Cameron

Reputation: 28783

Show popup in centre of screen/browser

<html>
<head>
<script language="javascript" type="text/javascript" >

function popupwindow(url, title, w, h)
{
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    var new_left = window.screenX + (((window.outerWidth/2) - (w/2)));
    var new_top = window.screenY + (((window.outerHeight/2) - (w/2)));

    return window.open(url, title, 'width='+w+', height='+h+', top='+new_top+', left='+new_left+',toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no');
}

popupwindow('index.html','','1024','768');


</script>
</head>
<body>

</body>
<html>

Upvotes: 0

Views: 4478

Answers (3)

Tushar Ahirrao
Tushar Ahirrao

Reputation: 13115

You can do this using following code :

function centerWindow() {
leftPos = 0
topPos = 0
if (screen) {
leftPos = (screen.width / 2) - 251
topPos = (screen.height / 2) - 162
}
ElementWindow = window.open('index.html','_blank',
'width=502,height=325,left='+leftPos+',top='+topPos)

} 

Upvotes: 0

epascarello
epascarello

Reputation: 207501

Look at what you posted

newwindow = window.open('index.html','_blank', 'top=0,left=0,width=1024,height=768,top='+centeredY+',left='+centeredX+'');

You have top=0,left=0 and top='+centeredY+',left='+centeredX

Why do you have it twice?

Also the code is not cross browser friendly.

Upvotes: 1

Fabio
Fabio

Reputation: 19176

You have specified top and left values more than once in your argument list

function popup()
{
    newwindow = window.open('index.html','_blank', 'top=0,left=0,width=1024,height=768,top='+centeredY+',left='+centeredX+'');
                                                    ^^^^^^^^^^^^^
    if (window.focus) {newwindow.focus()}
}

Upvotes: 0

Related Questions