Scenario:
Is there any way in JavaScript to get the popup window to open on the same monitor as the initial browser window (the opener)?
The syntax to open a popup is: window. open(url, name, params) : url. An URL to load into the new window.
focus()"> window. focus(); document. focus(); this. focus();
JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
You can't specify the monitor, but you can specify the position of the popup window as being relative to the where the click caused the window to popup.
Use the getMouseXY() function to get values to pass as the left and top args to the window.open() method. (the left and top args only work with V3 and up browsers).
window.open docs: http://www.javascripter.net/faq/openinga.htm
function getMouseXY( e ) { if ( event.clientX ) { // Grab the x-y pos.s if browser is IE. CurrentLeft = event.clientX + document.body.scrollLeft; CurrentTop = event.clientY + document.body.scrollTop; } else { // Grab the x-y pos.s if browser isn't IE. CurrentLeft = e.pageX; CurrentTop = e.pageY; } if ( CurrentLeft < 0 ) { CurrentLeft = 0; }; if ( CurrentTop < 0 ) { CurrentTop = 0; }; return true; }
Here is something I shamelessly reverse engineered from the Facebook oauth API. Tested on a primary and secondary monitor in Firefox/Chrome.
function popup_params(width, height) { var a = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft; var i = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop; var g = typeof window.outerWidth!='undefined' ? window.outerWidth : document.documentElement.clientWidth; var f = typeof window.outerHeight != 'undefined' ? window.outerHeight: (document.documentElement.clientHeight - 22); var h = (a < 0) ? window.screen.width + a : a; var left = parseInt(h + ((g - width) / 2), 10); var top = parseInt(i + ((f-height) / 2.5), 10); return 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=1'; } window.open(url, "window name", "location=1,toolbar=0," + popup_params(modal_width, modal_height));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With