Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get JavaScript to open a popup window on the current monitor

Tags:

Scenario:

  1. The user has two monitors.
  2. Their browser is open on the secondary monitor.
  3. They click a link in the browser which calls window.open() with a specific top and left window offset.
  4. The popup window always opens on their primary monitor.

Is there any way in JavaScript to get the popup window to open on the same monitor as the initial browser window (the opener)?

like image 677
jeremcc Avatar asked Sep 11 '08 21:09

jeremcc


People also ask

How do I open a popup with JavaScript?

The syntax to open a popup is: window. open(url, name, params) : url. An URL to load into the new window.

How do I bring a pop-up window to front in HTML?

focus()"> window. focus(); document. focus(); this. focus();

What are the two pop-up windows in JavaScript?

JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.


2 Answers

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; } 
like image 74
rp. Avatar answered Oct 24 '22 09:10

rp.


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)); 
like image 44
Chase Seibert Avatar answered Oct 24 '22 10:10

Chase Seibert