Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 210
hunt Avatar asked Jun 03 '10 12:06

hunt


3 Answers

You can use the 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>
like image 67
Pranay Rana Avatar answered Nov 19 '22 07:11

Pranay Rana


focusedWindow = window.open('two.html','two').focus();

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

like image 9
RobertPitt Avatar answered Nov 19 '22 06:11

RobertPitt


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.

like image 6
Roman Avatar answered Nov 19 '22 08:11

Roman