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
Release: 2025-01-08 14:57:41
Original
964 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

And add this to the body tag of your popup window:

<code class="language-html">onload="fixform();"</code>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template