Nrc
Nrc

Reputation: 9787

Center a window.open in the screen (not the browser)

How to center a window open in the screen? I know there are other solutions but I think they center the new window in relation of the existing window, not the total screen of the computer. So, those solutions only work if the browser is full screen.

My example has that same problem, it only works if the first window is full screen. Is there any solution that works always?

Here is the example to play: http://jsfiddle.net/T7DJr/

(I tried screen.width but it does not work. Is it a jQuery problem?)

Html:

<a class="open" href="http://www.google.com" target="_blank"> open window </a>

jQuery:

$(function(){    
    // this only centers in relation to the browser, not the screen:
    var top = $(window).height()/2 - 200;
    var left = $(window).width()/2 - 200;

    $('.open').click(function(e){
        e.preventDefault();
        window.open("","window1",
                    "toolbar=no, scrollbars=no, resizable=no, top="+top+", left="+left+", width=400, height=400");
    }); 
})

Upvotes: 0

Views: 255

Answers (1)

luciole75w
luciole75w

Reputation: 1117

You can use

var top = window.screen.height/2 - 200;
var left = window.screen.width/2 - 200;

I am not sure about the browser compatibility though.

Upvotes: 2

Related Questions