jQuery open new window centered with fixed size

John picture John · Nov 7, 2012 · Viewed 40.4k times · Source

Possible Duplicate:
Center a popup window on screen?

I have 4 different links, all of which need to open a new window which will target 4 different html files.

When the links are clicked, it needs to open the html file in question in a new window, both:

  • Centered
  • Fixed size 900 x 600

I have found this below, but it doesnt seem to cater for centering of the window

http://jquerybyexample.blogspot.com/2012/05/open-link-in-new-tab-or-new-popup.html

Cheers

Answer

adeneo picture adeneo · Nov 7, 2012

To center the new window, give it a left and top value half of the screen size - half of the window size :

var left  = ($(window).width()/2)-(900/2),
    top   = ($(window).height()/2)-(600/2),
    popup = window.open ("", "popup", "width=900, height=600, top="+top+", left="+left);

FIDDLE