简体   繁体   English

重新加载页面后,如何跟踪JavaScript中的全局词典值?

[英]How can I keep track of global dictionary values in JavaScript after page reload?

I'm keeping track of child windows being opened from a parent page. 我一直在跟踪从父页面打开的子窗口。 The following code works as partially as expected, but with an important flaw. 以下代码部分按预期工作,但有一个重要缺陷。 Whenever I execute the postback, it reloads the JavaScript and it erases the global variable. 每当执行回发时,它都会重新加载JavaScript并擦除全局变量。

My proposed solution 我建议的解决方案

I thought of adding the object to the sessionStorage with the following line of code; 我想到了使用以下代码将对象添加到sessionStorage;

            sessionStorage.setItem(thiskey, winInst);

However, I'm getting an error: JavaScript runtime error: Circular reference in value argument not supported . 但是,我遇到一个错误: JavaScript运行时错误:不支持value参数中的循环引用

The value being added is a window object. 要添加的值是一个窗口对象。

Here's the working code without the sessionStorage line. 这是没有sessionStorage行的工作代码。

var openedWindows = {};

setInterval(checkIfWindowIsClose, 40);

function openThisWindow(popLoc, attributes, id) {
    var winInst = window.open(popLoc, attributes, id);
    openedWindows["clickedRow" + id] = winInst;
}

function checkIfWindowIsClose() {
    var id = "";
    for (var i in openedWindows) {

        if (openedWindows[i].closed) {
            console.log(i);
            delete openedWindows[i];
            id = (' ' + i).slice(1);
            id = id.replace("clickedRow", "");

        }
    }
    if (id !== "") {

        __doPostBack('upList.UniqueID', id.toString());
        id = "";
    }

}

Goal 目标

Keep track of the openedWindows dictionary after a page reload. 重新加载页面后,请跟踪已打开的Windows词典。

How can I achieve this with sessionStorage? 如何使用sessionStorage做到这一点?

Thank you, 谢谢,

sessionStorage and localStorage only store strings as values, while the window object cannot be serialised to a string. sessionStoragelocalStorage仅将字符串存储为值,而window对象无法序列化为字符串。

What you can do here is, store a reference to the name of the window object. 您可以在此处存储对窗口对象名称的引用。

The window.open function allows you to open a new window and give a name to it in the second parameter, you can then access and refocus on the said window using the window.open method by passing in the name as the second parameter, if a window with that name is not open already, it'll open a new window. window.open函数允许您打开一个新窗口并在第二个参数中为其命名,然后可以使用window.open方法通过将名称作为第二个参数传入来访问并重新聚焦于该窗口。具有该名称的窗口尚未打开,它将打开一个新窗口。

Instead of trying to save the complete window instance in the session storage, you can save the name of the window only. 您可以只保存窗口的名称,而不必尝试将完整的窗口实例保存在会话存储中。

Refer: Check if window is already open window.open 请参阅: 检查窗口是否已经打开window.open

I followed the advice presented in this post: 我遵循了这篇文章中提出的建议:

Javascript: persist window object refrence? Javascript:持久化窗口对象引用?

This is my code: 这是我的代码:

Parent 父级

    var openedWindows = {};
    var child = {};

    setInterval(checkIfWindowIsClose, 40);

    function openThisWindow(popLoc, attributes, id) {
        var winInst = window.open(popLoc, attributes, id);
        openedWindows["clickedRow" + id] = winInst;

    }

    function checkIfWindowIsClose() {
        var id = "";

        for (var i in openedWindows) {

            if (openedWindows[i].closed) {
                // i is the id of your window and here you know that your window with id has been closed
                // here remove also the window from the object otherwise you will keep also the instance of the closed one
                console.log(i);
                child = openedWindows;

                id = (' ' + i).slice(1);
                id = id.replace("clickedRow", "");
                delete openedWindows[i];
            }
        }

        if (id !== "") {

            __doPostBack('upList.UniqueID', id.toString());
            id = "";
        }
    }

    // Update openedWindows references if parent page (this page)
    // has reloaded. 
    function linkParent(window, id) {
        window.opener.child = window;
        var thiskey = "clickedRow" + id;
        openedWindows[thiskey] = window;

    }

    // Let the child windows know that they must
    // send back to this parent page; their prent- window references
    // for when parent page has reloaded.
    window.onunload = function() {
        if (child.length !== 0)
        {
            for (var i in child) {

                child[i].expire();

            }
        }

    };

Child 儿童

    // Execute call to function once two second has passed.
    function expire() {
        setTimeout(function () { referToTop(window.opener); }, 2000);
    }

    // Retrieve record being inspected, window object and sending those
    // parameters back to the parent page -> Parent.aspx
    function referToTop(thisOpener) {
        var id = getUrlParameter('id'); 
        thisOpener.top.linkParent(window, id);
    }

    // Retrieve query information from page.
    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

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

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