Home >Backend Development >C++ >How to Open a Response.Redirect in a New Browser Window?

How to Open a Response.Redirect in a New Browser Window?

Barbara Streisand
Barbara StreisandOriginal
2025-01-08 14:57:41965browse

How to Open a Response.Redirect in a New Browser Window?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn