开发者

Javascript for .NET 3.5: How to give textbox focus after a pop-up is closed

Is there a way to open a pop-up via window.open(....), let it stay on top of the main window, and upon its close, give focus to a text box on the calling page?

I have been tasked to adjust functionality written by someone else. The .NET 3.5 page currently allows restocking of inventory and upon a button click, a new popup displays a receipt. The html for the pop-up is stored in a hidden field and is opened by a call to a js function.

On the page is also a text box which I have been asked to keep focus in. I have it working except for when the receipt is displayed. If I call a server or client side function to maintain focus of textbox, 开发者_如何学编程the pop-up window appears on top of the main window for a second and then is overlayed by the main page because the textbox receives focus, and that is not what is desired.


Is it possible for you to take a different design and use something like a jQuery dialog for the receipt instead of posting back to the server? Maintaining the focus client-side will be much easier than trying to force it through the server-side.


A solution that might work for you is to have the pop-up page tell it's "parent" page to set focus to one of it's controls when it is getting closed. You can do this with javascript.

On your pop-up page, add this to the body tag

<body onbeforeunload='window.opener.getElementById("textbox_to_focus").focus();'>

Caveat: The onbeforeunload event fires whenever the user navigates away from the page (ie, postback, back button, close button, etc). If your pop-up page doesn't have any navigation or any post backs, then this should work for you.


It might not be the most efficient way, but you could open the window and have JS timer run on the main window with an interval of every second. Then use if(window.closed) to determine if the window has been closed. If it has, then set focus to the textbox and disable the timer.

Here's an example page of how the above could be implemented:

<html>
<head>
<script type="text/javascript">

testwindow = window.open("<your page>","window");

function timer()
{
   var t=setTimeout("checkWindow()",1000);
}

function checkWindow()
{

if(testwindow.closed)
   document.getElementById("tbox").focus(); 
else
   timer();

}

</script>

</head>
<body onload="timer()">
   <input name="tbox" />
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜