Redirecting to a New Browser Window
For a smoother user experience when using Response.Redirect
, opening the redirected page in a new browser tab or window is often preferred. This avoids disrupting the current page. A straightforward method avoids the complexity of using JavaScript's register script
function.
To achieve this, simply add a specific attribute to your server-side link or button:
<code class="language-html">OnClientClick="aspnetForm.target='_blank';"</code>
Here's an example using a button:
<code class="language-html"><asp:Button ID="myButton" runat="server" Text="Click Me!" OnClick="myButton_Click" OnClientClick="aspnetForm.target='_blank';" /></code>
In the server-side OnClick
event (myButton_Click
), execute your Response.Redirect
. The redirected page will now open in a new window.
To prevent unintended behavior where all links open in new windows, include this JavaScript function in the header of your popup window:
<code class="language-javascript">function fixform() { if (opener.document.getElementById("aspnetForm").target != "_blank") return; opener.document.getElementById("aspnetForm").target = ""; opener.document.getElementById("aspnetForm").action = opener.location.href; }</code>
And add this to the body tag of your popup window:
<code class="language-html">onload="fixform();"</code>
This ensures that only the intended links open in new windows.
The above is the detailed content of How to Open a Response.Redirect in a New Browser Window?. For more information, please follow other related articles on the PHP Chinese website!