views:

254

answers:

5

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  A: 

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>
Pranay Rana
well above code only works in IE but now in firefox 3.6
hunt
@hunt: nor Google Chrome. Focusing the window this way isn't cross browser compatible.
Andy E
Well it's "compatible" in that it won't hurt anything to try, but Firefox, Chrome, and Safari won't obey the request. Firefox can be configured by its user to allow `focus()` requests like that to work, but I think Chrome and Safari always ignore it. There's no way to force those browsers to pay attention. (In fact Safari is *really* serious about it: http://whatsthepointy.blogspot.com )
Pointy
using window.open('two.html','two') with or without focus in chrome wont make a difference as there built to open in a net TAB, not window.
RobertPitt
A: 

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.

Andy E
well this code will close my previously opened window , and opens it another. i don't want to close my previous window
hunt
@hunt: Like I said in my comment on [pranay_stacker's answer](http://stackoverflow.com/questions/2965754/set-the-focus-of-a-popup-window-everytime/2965817#2965817), the focus method will not work cross browser for popup windows. I'm fairly sure that there's no other way to refocus the window.
Andy E
A: 
focusedWindow = window.open('two.html','two').focus();

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

RobertPitt
A: 

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.

Sachin Shanbhag
A: 

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

James