We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


JavaScript - Preserving Window Position Between Sessions

stminfo asked
Medium Priority
Last Modified: 2009-12-16
I need to preserve the window (i.e., browser) position between sessions for a give web page.  So a user opens a particular web page (an ASP.NET page) and moves it around then closes it.  When the user opens that page again I want it to open at the same location that it was last at.  I can get the window’s position using window.screenLeft and window.screenTop and preserve this in a cookie.  I could then move the window to the former position on an onload event.  What I don’t know what to do is when to trigger this process.  What I need is something like an onmove event to trigger the process when the user moves the window.  Does such an event exist?  Or is there some better way to do this?  I am using ASP.NET 2.0, Internet Explorer and JavaScript.

Watch Question

I take it you mean to ask when to store the position? Use the body event onunload for this.
Michel PlungjanIT Expert
Top Expert 2009

That would not be useful since the position of the window is actually the hack to find when the window is closed because it gets a silly value like 6000 or so.
Better is to set a cookie at intervals if allowed. Set one and if that cookie is correctly set, set one every second or so or monitor when the window moves and set one when it stops


Well, you could always use the onresize event of the window. You have to set that in javascript though (not in HTML) like this:

window.onresize = function() {
  // set the cookie here


Thanks for your feedback thus far.

How do I set an interval to save the window position or how can I monitor window movement?

Using the onresize event wouldn't work since the window that I am concerned with is of a fixed size.

The window also has an onmove event:

window.onmove = function() {
  // set the cookie here

Does this help?


I don't think that Internet Explorer has a window.onmove event.

Based on some of the comments, I was able to come up with the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>Saving Position With Cookies</title>
    <script language="javascript" type="text/javascript">
    // Global variables
    var formerXPos = 0;
    var formerYPos = 0;
    // The following interval is how often the page will
    //   check if it's been moved (1000 = 1 second)
    var myInterval = window.setInterval("checkPosition()", 500);
    // This function get the specified cookie's value and returns
    //   as a number.
    //   returns NaN is not a number.
    function getTheCookieNumber(cookieName)
        var ret = NaN;
        var xStr = "";
        var theCookie = document.cookie;
        var startIdx = theCookie.indexOf(cookieName);
        var endIdx;
        if (startIdx != -1)
            endIdx = theCookie.indexOf(";", startIdx);
            if (endIdx == -1)
                // no ending semicolon, us end of string
                endIdx = theCookie.length;
            xStr = theCookie.substr(startIdx + cookieName.length, endIdx);            
            ret = parseInt(xStr, 10);          
            return ret;
    function myOnLoad()
       var xTheStr = "";
       var xCoordinate = -1;
       var yCoordinate = -1;
       // Check for former position cookie
        var xCookieStr = "FormerWinPosX=";
        var yCookieStr = "FormerWinPosY=";
        xCoordinate = getTheCookieNumber(xCookieStr);
        yCoordinate = getTheCookieNumber(yCookieStr);
        if ((isNaN(xCoordinate)) || (isNaN(yCoordinate)))
            // No cookies set coordinates to middle bottom of the screen
            xCoordinate = (screen.availWidth - WindWidth) / 2;
            yCoordinate = screen.availHeight - WindHeight;
            // Use the former position cookie
            // Adjust for border            
            xCoordinate = xCoordinate - 3;
            yCoordinate = yCoordinate - 10;
         window.moveTo(xCoordinate, yCoordinate);
    function checkPosition()
        var expiresOn = new Date();
        if ((formerXPos != window.screenLeft) ||
             (formerYPos != window.screenTop))
            // set cookie expiration for 6 months;
            expiresOn.setMonth(expiresOn.getMonth() + 6);        
            document.cookie = "FormerWinPosX=" + window.screenLeft + ";expires=" + expiresOn + ";";
            document.cookie = "FormerWinPosY=" + window.screenTop + ";expires=" + expiresOn + ";";
            // Save position
            formerXPos = window.screenLeft;
            formerYPos = window.screenTop;
<body onload="myOnLoad()">
        This is sample page of how to save position!</div>


The one problem is that this code is not generic.  window.screenLeft and window.screenTop give the coordinates of the client area and so you need to adjust for the boarders.  In in my case, I have a set window size (not included in the code above) so I can hard code the adjustment.  It would be nice to be able to not hard code the adjustment.

Does anyone know of a way to get the coordinates of the entire browser window (i.e. Internet  Explorer) and not just the client area?
IT Expert
Top Expert 2009
If you open the window yourself the very first time, you know where it is, then you can use the screenLeft and top to calculate where to open it next time

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.