Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>// Pops a window relative to the current window position function popup(url, winName, xOffset, yOffset) { var x = (window.screenX || window.screenLeft || 0) + (xOffset || 0); var y = (window.screenY || window.screenTop || 0) + (yOffset || 0); return window.open(url, winName, 'top=' +y+ ',left=' +x)) } </code></pre> <p>Call it like the following and it will open on top of the current window</p> <pre><code>popup('http://www.google.com', 'my-win'); </code></pre> <p>Or make it slightly offset</p> <pre><code>popup('http://www.google.com', 'my-win', 30, 30); </code></pre> <p>The point is that window.screenX/screenLeft give you the position in relationship to the entire desktop, not just the monitor.</p> <p>window.screen.left would be the ideal candidate to give you the information you need. The problem is that it's set when the page is loaded and the user could move the window to the other monitor.</p> <p><strong>More research</strong> </p> <p>A final solution to this problem (beyond just offsetting from the current window position) requires knowing the dimensions of the screen that the window is in. Since the screen object doesn't update as the user moves a window around, we need to craft our own way of detecting the current screen resolution. Here's what I came up with</p> <pre><code>/** * Finds the screen element for the monitor that the browser window is currently in. * This is required because window.screen is the screen that the page was originally * loaded in. This method works even after the window has been moved across monitors. * * @param {function} cb The function that will be called (asynchronously) once the screen * object has been discovered. It will be passed a single argument, the screen object. */ function getScreenProps (cb) { if (!window.frames.testiframe) { var iframeEl = document.createElement('iframe'); iframeEl.name = 'testiframe'; iframeEl.src = "about:blank"; iframeEl.id = 'iframe-test' document.body.appendChild(iframeEl); } // Callback when the iframe finishes reloading, it will have the // correct screen object document.getElementById('iframe-test').onload = function() { cb( window.frames.testiframe.screen ); delete document.getElementById('iframe-test').onload; }; // reload the iframe so that the screen object is reloaded window.frames.testiframe.location.reload(); }; </code></pre> <p>So if you wanted to always open the window at the top left of whatever monitor the window is in, you could use the following:</p> <pre><code>function openAtTopLeftOfSameMonitor(url, winName) { getScreenProps(function(scr){ window.open(url, winName, 'top=0,left=' + scr.left); }) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload