How to focus on JS popups in Firefox & IE?

倾然丶 夕夏残阳落幕 提交于 2019-12-14 03:59:01

问题


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);
    }
}​

来源:https://stackoverflow.com/questions/9999802/how-to-focus-on-js-popups-in-firefox-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!