Javascript:检查重复打开的窗口 [英] Javascript: Check for duplicate opened window
问题描述
是否可以检查是否已经打开了同一个窗口?
Is it possible to check if same window is already opened?
例如我已经通过javascript打开了一个窗口。
For example i have opened a window via javascript.
我可以通过javascript检查是否在另一个页面上打开了吗?
Can i check if that's opened on another page via javascript?
如果已经打开它以避免重复的窗口,那么只想关注页面。
Just want to focus on page if it's been opened already to avoid duplicate windows.
谢谢;)
推荐答案
查看窗口.open()
方法。您必须将窗口的名称指定为第二个参数。如果已有一个具有该名称的窗口,则新URL将在现有窗口中打开,请参阅 http://www.w3schools.com/jsref/met_win_open.asp
Look at window.open()
method. You must specify the name of the window as the second parameter. If there already is a window with that name, then the new URL will be opened in the already existing window, see http://www.w3schools.com/jsref/met_win_open.asp
如果你真的想检查,如果你自己打开了窗户脚本,然后你必须在全局变量或类似的东西中保持对已打开窗口的引用并使用
If you really want to check, if the window is opened by your own scripts, then you have to keep a reference to the opened window in a global variable or the likes and create it with
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
您还可以在方法中封装此行为:
You can also encapsulate this behavior in a method:
var myOpenWindow = function(URL) {
var myOpenedWindow = myOpenedWindow || window.open(URL, "MyNewWindow");
myOpenedWindow.location.href= URL;
myOpenedWindow.focus();
}
用 myOpenWindow调用该函数('http: //www.example.com /');
这篇关于Javascript:检查重复打开的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!