Click here to Skip to main content
15,885,182 members
Articles / Server
Article

Bring the window.open() window on front

Rate me:
Please Sign up or sign in to vote.
4.89/5 (2 votes)
11 Oct 2013CPOL1 min read 28K   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');<br />
NewWin.focus();

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

<head runat="server"><br />    <script type="text/javascript"><br />        window.focus();<br />    </script><br />    <title>Untitled Page</title><br /></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');<br />NewWin.focus();<br />
setTimeout('ExecFocus()', 60);<br />function ExecFocus() { <br /> NewWin.focus(); <br />}

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --