How to position a pop up to be in the top right part of the screen

14,426

Solution 1

var viewportwidth = document.documentElement.clientWidth;
var viewportheight = document.documentElement.clientHeight;
window.resizeBy(-300,0);
window.moveTo(0,0);

window.open("http://google.com",
            "mywindow",
            "width=300,left="+(viewportwidth-300)+",top=0");

Solution 2

I haven't tested out the actual window size math; not sure if that's correct. But the first, obvious, problem you have is embedding the variables into the call to window.open. Try changing

  window.open("something.htm", "mywindow",
 "width=300, height=viewportheight, left=(viewportwidth - 300), top=0, screenX=0, screenY=0"); 

to

  window.open("something.htm", "mywindow",
 "width=300, height=" + viewportheight + ", left=" + (viewportwidth - 300) + ", top=0, screenX=0, screenY=0");

Basically, if you want the variables or the math to get resolved, they have to be outside the string.

Share:
14,426

Related videos on Youtube

user220755
Author by

user220755

Updated on June 04, 2022

Comments

  • user220755
    user220755 almost 2 years

    What I want to do is to change the size of a window and create a window next to it so they are exactly next to each other. In that sense, I need to position to the new window to the top right part of the screen, the way I am doing it is not working (code is below), I need help :)

    function () { 
       var viewportwidth = document.documentElement.clientWidth;
       var viewportheight = document.documentElement.clientHeight;
       window.resizeBy(-300,0); 
       window.open("something.htm",
         "mywindow",
         "width=300,
         height=viewportheight,
         left=(viewportwidth - 300),
         top=0,
         screenX=0,
         screenY=0"); 
    }