detecting the existence of window after reload

天大地大妈咪最大 提交于 2019-12-05 11:29:53

The best way to keep a LITTLE bit of state without storing in cookies is to save something to your window.name. It'll only work with a string, though, so you'll need to save it as a JSON string if you want more than just a single bit of data.

To read:

var keptState;
if (window.name !== "") {
    keptState = JSON.parse(window.name);
}

To write:

state = {
    "popupOpened" : true,
    "popupName" : "otherWindowName"
};
window.name = JSON.stringify(state);

On the other side of the refresh (or even navigating away from the site and back), it'll keep your variables you wanted. If you actually wanted a reference to the other window, however, you're on your own for that. I can't think of any way to determine that it's actually there. This would be a problem if, say, someone closed the popup window on their own.

At least this way, you can find out if you've opened it. Then you could do a popunder if you really needed that window without the popup. This always feels a little shady when I do it, but it's probably the best way to do what you're asking for.

var myWindow = window.open('', 'otherWindowName');
myWindow.blur();
window.focus();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!