问题
I have two pages one.html and 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.
note: I have set popup window's name as 'two'
回答1:
you can achive by using focus
function as used below
<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>
回答2:
focusedWindow = window.open('two.html','two').focus();
Directly append the focus() to the window.open protoype
回答3:
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.
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.
回答4:
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.
回答5:
var win = window.open('two.html','two');
win.document.getElementsByTagName("body")[0].focus(); //focus the body rather than the window.
回答6:
Check your Firefox 2 settings at
- Go to Tools -> Options -> Content
- Make sure "Enable JavaScript" is turned on.
- Next to "Enable JavaScript", click the "Advanced" button.
- Make sure that there is a check-mark in the box [x] Raise or lower windows
If the setting "Raise or lower windows" is turned off, then this disables the window.focus() method.
回答7:
Unfortunately some modern browsers still don't support focusing tabs, see this Firefox bug entry, for example.
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 two
is already there, no new window/tab is opened, but it's reused.
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.
However, directly after using window.open
, document.hasFocus()
always returns true (so said, also in Chrome). The workaround is to use setTimeout
one ms later.
回答8:
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.
回答9:
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.
回答10:
I found an answer for myself that works. In the html of the popup body, add to the body tag onload="this.window.focus()". Even if you minimize the window, it pops back up front and center. Be sure and name the window in your execution script window.open('url','some name','vars'); Hope this helps you!
来源:https://stackoverflow.com/questions/2965754/set-the-focus-of-a-popup-window-every-time