Javascript 每次设置弹出窗口的焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2965754/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:42:51  来源:igfitidea点击:

set the focus of a popup window every time

javascriptpopup

提问by hunt

I have two pages one.html and two.html

我有两页 one.html 和 two.html

I am opening a new window using following code

我正在使用以下代码打开一个新窗口

//here popup is a global variable
popup=window.open('two.html','two');

for the first time a popup window open successfully and get the focus but if I try to open it again without closing already opened popup then two.html is not getting focus for the second time.

第一次弹出窗口成功打开并获得焦点,但如果我尝试再次打开它而不关闭已打开的弹出窗口,那么 two.html 不会第二次获得焦点。

note: I have set popup window's name as 'two'

注意:我已将弹出窗口的名称设置为“两个”

回答by Pranay Rana

you can achive by using focusfunction as used below

您可以通过使用focus下面使用的功能来实现

<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
    newwindow=window.open(url,'name','height=200,width=150');
    if (window.focus) {newwindow.focus()}

      if (!newwindow.closed) {newwindow.focus()}
    return false;
}

// -->
</script>

回答by RobertPitt

focusedWindow = window.open('two.html','two').focus();

Directly append the focus() to the window.open protoype

直接将 focus() 附加到 window.open 原型

回答by Roman

My empiric solution that always works:

我的经验解决方案始终有效:

setTimeout('detail.focus()', 1);

Appending .focus()right after open does not always work, nor calling a .focus()without setTimeout.

.focus()在 open 之后立即追加并不总是有效,也不总是在.focus()没有 setTimeout的情况下调用。

I know it's ugly (wanted to get rid of it right now, after some years, with no success), and I do not know the exact reasons why and how, but it works.

我知道它很丑(我想现在摆脱它,几年后,没有成功),我不知道为什么和如何的确切原因,但它有效。

回答by Andy E

You can close the popup before you open it if you check to see if the popup is already open when you call the function.

如果在调用该函数时检查弹出窗口是否已经打开,则可以在打开它之前关闭该弹出窗口。

var popup;
?function openPop() { 
    if ("close" in popup) popup.close();
    popup = window.open("http://stackoverflow.com", "test", "width=200, height=200");????????
}

?This will ensure the popup always appears on top.

?这将确保弹出窗口始终显示在顶部。

回答by James

var win = window.open('two.html','two');
win.document.getElementsByTagName("body")[0].focus(); //focus the body rather than the window.

回答by sibbl

Unfortunately some modern browsers still don't support focusing tabs, see this Firefox bug entry, for example.

不幸的是,一些现代浏览器仍然不支持聚焦选项卡,例如,请参阅此 Firefox 错误条目

However, the following code is working well for me. As a workaround, it reopens a window/tab if it cannot be focussed:

但是,以下代码对我来说效果很好。作为一种解决方法,如果无法聚焦,它会重新打开窗口/选项卡:

  var win = window.open("two.html", "two")
  win.focus();
  setTimeout(function() {
    if(document.hasFocus()) {
      win.close();
      window.open("two.html", "two")
    }
  }, 1);

In the first line it opens the window and tries to focus it using the second line. If a window with the name twois already there, no new window/tab is opened, but it's reused.

在第一行中,它打开窗口并尝试使用第二行将其聚焦。如果two已存在具有该名称的窗口,则不会打开新窗口/选项卡,但会重复使用它。

The trick now is, that we check if the current window still has focus using document.hasFocus(). If so, we close the window and reopen it. This is only for browsers which don't support focusing the tab which is to be reused directly. Currently, this is the case for FF and IE/MS Edge. Chrome works well.

现在的诀窍是,我们使用document.hasFocus(). 如果是这样,我们关闭窗口并重新打开它。这仅适用于不支持聚焦要直接重用的选项卡的浏览器。目前,FF 和 IE/MS Edge 就是这种情况。铬效果很好。

However, directly after using window.open, document.hasFocus()always returns true (so said, also in Chrome). The workaround is to use setTimeoutone ms later.

但是,在使用之后window.opendocument.hasFocus()总是返回 true(如此说,也在 Chrome 中)。解决方法是在setTimeout1 毫秒后使用。

回答by thaiiceland

just add .focus() to the window.open line window.open('two.html','two').focus()

只需将 .focus() 添加到 window.open 行 window.open('two.html','two').focus()

I did have same problem and this seems to work in both Firefox and Chromium web browsers. In my web thaiiceland.comI noticed that if the popup was open, the focus didn't go on the popup and you feel nothing happen when you click on that link. So I search and after try to just add .focus() in the end of the window.open line it works.

我确实遇到了同样的问题,这似乎在 Firefox 和 Chromium 网络浏览器中都有效。在我的网站thaiiceland.com 中,我注意到如果弹出窗口是打开的,焦点不会放在弹出窗口上,当您单击该链接时,您会感觉没有任何反应。所以我搜索并尝试在 window.open 行的末尾添加 .focus() 之后它就可以工作了。

<a href="currency.html" onclick="javascript:void window.open('currency.html','thaiice02','width=900,height=560,scrollbars=1,resizable=1,top=80,left=150').focus();return false;">Curency converter - ????????????</a>

回答by Sachin Shanbhag

Check your Firefox 2 settings at

检查您的 Firefox 2 设置

  1. Go to Tools -> Options -> Content
  2. Make sure "Enable JavaScript" is turned on.
  3. Next to "Enable JavaScript", click the "Advanced" button.
  4. Make sure that there is a check-mark in the box [x] Raise or lower windows
  1. 转到工具 -> 选项 -> 内容
  2. 确保“启用 JavaScript”已打开。
  3. 在“启用 JavaScript”旁边,单击“高级”按钮。
  4. 确保复选框 [x] 升高或降低窗户中有复选标记

If the setting "Raise or lower windows" is turned off, then this disables the window.focus() method.

如果关闭“升高或降低窗口”设置,则会禁用 window.focus() 方法。

回答by user2338552

setTimeout(function () { 
  window.open('PageName', '', 'height=560,width=1120,resizable=no,left=100,top=50,screenX=100,screenY=50'); 
}, 100);

-> This code is use for giving focus on newly open window. -> Put it in javascript.('setTimeout is inbuilt function of javascript') -> Many time happen that when popup window is opened, it will go behind the old window, So above is very useful to focus for new window.

-> 此代码用于将焦点放在新打开的窗口上。-> 把它放在javascript中。('setTimeout 是javascript的内置函数') -> 很多时候,当弹出窗口打开时,它会跑到旧窗口的后面,所以上面对新窗口的焦点非常有用。

回答by dishwasherWithProgrammingSkill

It is a one liner solution, use focus on your pop up var:

这是一个单行解决方案,使用焦点在你的弹出变量上:

popup=window.open('two.html','two');
popup.focus();

This will always open your pop up window even if it is not closed.

即使它没有关闭,这也将始终打开您的弹出窗口。