最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bring popup to front - Stack Overflow

programmeradmin0浏览0评论

In my application, I have a popup with information that open when I choose some options.

First time it's OK, popup highlights in front of everything.

But, when it loses focus, when the user goes to other window, if user clicks again in the same option, I want that the popup shows up again, in front of everything.

I tried something like:

<body onLoad="this.focus()">
window.focus();   
document.focus();  
this.focus();  

but it does not work for me.

What am I missing ?

In my application, I have a popup with information that open when I choose some options.

First time it's OK, popup highlights in front of everything.

But, when it loses focus, when the user goes to other window, if user clicks again in the same option, I want that the popup shows up again, in front of everything.

I tried something like:

<body onLoad="this.focus()">
window.focus();   
document.focus();  
this.focus();  

but it does not work for me.

What am I missing ?

Share Improve this question edited May 14, 2020 at 14:44 krlzlx 5,82214 gold badges49 silver badges59 bronze badges asked Jan 25, 2012 at 19:19 Ricardo BinnsRicardo Binns 3,2466 gold badges45 silver badges71 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

You should maintain a reference to the popup window you open and call focus method on it.

var win = window.open(url, name, args);
win.focus();

While opening a popup if you give a name and next time when you open a popup with the same name it will use the same popup if it is not closed. You just have to focus it.

You can use this simple function to handle popups

function windowOpener(url, name, args) {

    if(typeof(popupWin) != "object" || popupWin.closed)  { 
        popupWin =  window.open(url, name, args); 
    } 
    else{ 
        popupWin.location.href = url; 
    }

    popupWin.focus(); 
 }
发布评论

评论列表(0)

  1. 暂无评论