简体   繁体   English

用Javascript替换window.sessionStorage?

[英]Replacement for window.sessionStorage in Javascript?

I have an application with a launch page that needs to determine what is already opened, so it does not reopen things that are opened already in another new tab. 我有一个带有启动页面的应用程序,该应用程序需要确定已打开的内容,因此它不会在另一个新选项卡中重新打开已打开的内容。 In Firefox, I was able to make this work, by using window.sessionStorage to store the titles of pages that are open, and then use window.opener with the following code to remove the titles from the list. 在Firefox中,通过使用window.sessionStorage存储打开的页面的标题,然后使用带有以下代码的window.opener从列表中删除标题,我能够做到这一点。

Gecko Session Storage Info Page Gecko会话存储信息页面

if (window.sessionStorage) {
    if (window.sessionStorage.getItem(code)) {
        return; // page already open
    }
    else {
       window.sessionStorage.setItem(code, code);
       window.open("Sheet.aspx", "_blank");
    }
}

And on the pages that are opened: 在打开的页面上:

function signalPageExit() {
    if (window.opener.sessionStorage) {

    window.opener.sessionStorage.removeItem(
     document.getElementById("runcode").childNodes[0].textContent); 
}

This doesn't work in IE so I decided to use a cookie strategy, but the cookies were never successfully deleted from code on the dynamically launched pages, and therefore pages couldn't be reopened from the launch page once they had been launched until the cookie expired. 这在IE中不起作用,因此我决定使用cookie策略,但是cookie从未成功从动态启动页面上的代码中删除,因此一旦启动页面,就无法从启动页面重新打开页面,直到Cookie已过期。

My second attempt was to define my own sessionStorage when it did not exist. 我的第二个尝试是定义不存在的sessionStorage。 That looked like this: 看起来像这样:

    function setStoreItem(name, val) {
        this.storage[name] = val;
    }

    function getStoreItem(name) {
        return(this.storage[name]);
    }

    function removeStoreItem(name) {
        this.storage[name] = null;
    }

    function sesStorage() {
        this.storage = new storageData();
        this.setItem = setStoreItem;
        this.getItem = getStoreItem;
        this.removeItem = removeStoreItem;
    }

    // storage object type declaration
    function storageData() {

    }

    // IE 7 and others
    else {
        window.sessionStorage = new sesStorage();

        window.sessionStorage.setItem(code, code);
        window.open("Sheet.aspx", "_blank");
    }

But it seems the real session storage is special, this ordinary object of the window did not stay alive across postbacks and therefore when my launch page posted back, the list of created page titles was wiped out. 但是似乎真正的会话存储是特殊的,窗口的这个普通对象在回发之间并没有保持活动状态,因此,当我的启动页面回发时,创建的页面标题列表就被清除了。

So now I'm looking for a way to make this work. 所以现在我正在寻找一种使这项工作可行的方法。 I have a launch page called scoresheets.aspx that creates dynamic pages based on user requests. 我有一个名为scoresheets.aspx的启动页面,该页面根据用户请求创建动态页面。 These pages share a substantial amount of javascript code that can be modified to make this work. 这些页面共享大量的javascript代码,可以对其进行修改以使其正常工作。

I don't want to refresh the launched pages when a user tries to reopen them, but if there is some way to detect the titles of opened pages or some other way to use window.opener to communicate with the same persistence that sessionStorage has, I'd be glad to use it. 当用户尝试重新打开启动的页面时,我不想刷新启动的页面,但是如果有某种方法可以检测打开的页面的标题,或者通过其他方式使用window.opener与sessionStorage具有相同的持久性进行通信,我很高兴使用它。

Eric GarsidejStore插件为几个客户端存储引擎提供了一个基于jquery的api。

you should go with that cookie strategy and set those cookies to expire when the windows (tab) is closed. 您应该使用该Cookie策略,并将这些Cookie设置为在关闭窗口(标签)时过期。 that should work across browsers. 应该可以跨浏览器运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM