简体   繁体   中英

set the focus of a popup window every time

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'

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>
focusedWindow = window.open('two.html','two').focus();

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

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.

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.

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

Check your Firefox 2 settings at

  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

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

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!

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.

just add .focus() to the window.open line 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.com I 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.

<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>
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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM