windows Javascript:检查重复打开的窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4638910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Check for duplicate opened window
提问by Somebody
Is it possible to check if same window is already opened?
是否可以检查同一个窗口是否已经打开?
For example i have opened a window via javascript.
例如,我通过 javascript 打开了一个窗口。
Can i check if that's opened on another page via javascript?
我可以检查它是否通过 javascript 在另一个页面上打开吗?
Just want to focus on page if it's been opened already to avoid duplicate windows.
如果页面已经打开,只想专注于页面以避免重复窗口。
Thanks ;)
谢谢 ;)
回答by Residuum
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
看window.open()
方法。您必须指定窗口的名称作为第二个参数。如果已经有一个同名的窗口,那么新的 URL 将在已经存在的窗口中打开,参见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();
}
And call that function with myOpenWindow('http://www.example.com/');
并调用该函数 myOpenWindow('http://www.example.com/');
回答by Harry Joy
If you have parent--child window then here is a solution that will allow you to check to see if a child window is open from the parent that launched it. This will bring a focus to the child window without reloading its data:
如果您有父子窗口,那么这里有一个解决方案,可让您检查子窗口是否从启动它的父窗口打开。这将在不重新加载其数据的情况下将焦点带到子窗口:
<script type="text/javascript">
var popWin;
function popPage(url)
{
if (popWin &! popWin.closed && popWin.focus){
popWin.focus();
} else {
popWin = window.open(url,'','width=800,height=600');
}
}
</script>
<a href="http://www.xzy.com"
onclick="popPage(this.href);return false;">link</a>
one more thing ::--- If the user refreshes the parent window, it may loses all its references to any child windows it may have had open.
还有一件事 ::--- 如果用户刷新父窗口,它可能会丢失对它可能已打开的任何子窗口的所有引用。
Hope this helps and let me know the output.
希望这会有所帮助,并让我知道输出。
回答by SRTECH
This will help if you want to open a url from a link
如果您想从链接打开网址,这将有所帮助
var Win=null;
function newTab(url){
//var Win; // this will hold our opened window
// first check to see if the window already exists
if (Win != null) {
// the window has already been created, but did the user close it?
// if so, then reopen it. Otherwise make it the active window.
if (!Win.closed) {
Win.close();
// return winObj;
}
// otherwise fall through to the code below to re-open the window
}
// if we get here, then the window hasn't been created yet, or it
// was closed by the user.
Win = window.open(url);
return Win;
}
newTab('index.html');