保存窗口位置是爬行

时间:2017-03-20 23:42:33

标签: javascript cookies

我有一个Web应用程序,我正在存储窗口位置的cookie。

当用户关闭窗口时,Cookie会更新:

window.onbeforeunload = function () {
  var winLeft = window.screenX;
  var winTop = window.screenY;
  var winWidth = window.outerWidth;
  var winHeight = window.outerHeight;

  writeWindowCookie(winLeft, winTop, winWidth, winHeight);
}

var writeWindowCookie = function(left, top, width, height) {
   var prependStr = "Window";
   var date = new Date(new Date().setFullYear(new Date().getFullYear() + 1))
   var expires = "; expires=" + date.toGMTString();

   document.cookie = prependStr + "Left=" + left + "; " + expires + "; path=/";
   document.cookie = prependStr + "Top=" + top + "; " + expires + "; path=/";
   document.cookie = prependStr + "Width=" + width + "; " + expires + "; path=/";
   document.cookie = prependStr + "Height=" + height + "; " + expires + "; path=/";
}

  var readCookie = function (name) {
    var nameEQ = name + "=",
        ca = document.cookie.split(';'),
        i, c;

    for (i = 0; i < ca.length; i++) {
        c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }

        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

当我重新启动时,我会读取cookie然后调用Window Open:

var winLeft = readCookie("WindowLeft");
var winTop = readCookie("WindowTop");
var winWidth = readCookie("WindowWidth");
var winHeight = readCookie("WindowHeight");

window.open("", 'MyWindow', 'channelmode=0,scrollbars=1,Resizable=1,toolbar=0,menubar=0,status=0,left=' + winLeft + 'top=' + winTop + 'width=' + winWidth + 'height=' + winHeight, true);

问题在于每次重新打开时,宽度和高度都相同,但左侧和上侧移动。左边每次移动约6px。

我只是使用错误的函数来捕获X / Y坐标?

0 个答案:

没有答案