Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Bring the window.open() window on front

0.00/5 (No votes)
1 Jun 2011 1  
In asp.net application, new windows can be opened using window.open method either from the client side or from executing it from server side. See the

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

In asp.net application, new windows can be opened using window.open method either from the client side or from executing it from server side. See the list of parameters it takes http://msdn.microsoft.com/en-us/library/ms536651(v=vs.85).aspx

The problem most of the developers face with this, is that the new window does not popup in front of the opener window.

There are solutions for this, like;

- Setting the focus to the opened winow

 
var NewWin = window.open('somepage.aspx, 'myWin', 'toolbar=no, status=yes, menubar=no, resizable=no, scrollbars=yes, width=670, height=800');
NewWin.focus();

- Or in the load of new winodw's page (like in the header tag)

<head runat="server">
    <script type="text/javascript">
        window.focus();
    </script>
    <title>Untitled Page</title>
</head>

But most of the times these solutions fail, there are timing issues like the opened page comes back from the server and is shown again or there are still more events to happen on the opener page so even after the new window is shown, the opener page may come in front of you after the whole excution.

To bring the new page on the front and to encounter these type of issues, setting a time out on the script of focusing the new page will work.

You can set the time out (say 60 mili-seconds) to a method which will set the focus to the new opened window after it is opened. This way it can be made to appear on the front after all the processing on the opener page.

Use this script for opening the new window and bringing it on the front ;

var NewWin = window.open('somepage.aspx, 'NewWindow', 'toolbar=no, status=no, menubar=no, resizable=no, scrollbars=yes, width=670, height=800');
NewWin.focus();
setTimeout('ExecFocus()', 60);
function ExecFocus() {
NewWin.focus();
}

See parameters and examples of setTimeout method here: http://msdn.microsoft.com/en-us/library/ms536753(v=vs.85).aspx

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here