重装后的检测窗口的存在 [英] detecting the existence of window after reload
问题描述
因此,假设我这样做
so suppose I do this
var myWindow = false;
$('someelement').click(function(){
var myWindow = window.open('url', 'name', 'width=100,height=200');
});
让用户单击窗口中的元素和新的窗口(称之为窗口B)打开...
so the user clicks on the element on window A and a new window (call it window B) is opened...
但假设一个窗口重新加载得到...缘于此,VAR mywindow的现在设置为假的... ...但随后的窗口B是仍然打开....我怎么能检测窗口B的存在,从一个窗口后一个窗口重新加载得到
but then suppose window A gets reloaded...due to this, var myWindow is now set to false...but then window B is still open....how can I detect the existence of Window B from window A after window A gets reloaded
我知道你可以做到这一点:
I know that you can do this:
var hasWindow = window.open('', 'name');
和它会返回到窗口B中的引用,如果它已经打开。
and it will return a reference to window B if it's already open..
不过,如果窗口B尚未开通,这将触发而不是一个窗口开放,这是不可取的。
But then if window B is NOT opened yet, this will instead trigger a window opening, which is undesirable
我怎样才能到窗口B中的引用,如果它是开放的,而不会触发事件打开新窗口中的窗口B未开? (而不诉诸cookie的招数)
How can I get a reference to window B if it's open without triggering a new window opening in the event in which window B is not open? (without resorting to cookie tricks)
推荐答案
要保持状态一点点在没有存储的cookie,最好的办法是保存的东西你window.name。这将只有一个字符串工作,虽然,所以你需要将其保存为一个JSON字符串,如果你想比的数据只是一个单一的多一点。
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.
要阅读:
var keptState;
if (window.name !== "") {
keptState = JSON.parse(window.name);
}
要这样写:
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();
这篇关于重装后的检测窗口的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!