How to focus on JS popups in Firefox & IE?

2019-07-29 11:12发布

问题:

I've got a page that must open a popup window on another domain. When the link is clicked on the original page I'd liek it to focus on the new window if it's open, and open it if it's not.

This code works in Chrome, but I get nothing for Firefox or IE.

var win=null;

function pop(){
    if(win!== null && !win.closed) {
        //If it's already open, just focus on it
        win.focus();    
    }else {
        //Otherwise, make a new window and go to it
        win= window.open("http://google.com");
    }
}​

Demo here: http://jsfiddle.net/T7PUN/

Any ideas on how to achieve this across all browsers?



EDIT

This doesn't seem like the greatest solution, but it works. Chrome seems to support focusing on the window, while the other browsers don't. So for other browser closing and re-opening the window seems to work and have the same end result. I know it's not the greatest.

var win=null;

function pop(url){
    if(win!== null && !win.closed) {
        if(navigator.userAgent.indexOf("Chrome")>0) {
            //If it's already open, just focus on it
            win.focus();
        }else {
            //LAME
            win.close();
            win= window.open(url);
        } 
    }else {
        //Otherwise, make a new window and go to it
        win= window.open(url);
    }
}​