Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Target=_blank on a response.redirect?

Tags:

c#

.net

asp.net

I do not necessarily have to use response.redirect, but that is what I had. I would like to open the selected link in a new window. How do I do that?

context.Response.Redirect(ConfigurationManager.AppSettings["URL"] + ext );
like image 962
MrM Avatar asked Jan 24 '12 21:01

MrM


People also ask

Why target =_ blank is deprecated?

If you use target="_blank" only to open links in a new tab, then it is vulnerable to an attacker. When you open a link in a new tab ( target="_blank" ), the page that opens in a new tab can access the initial tab and change its location using the window.

How do I use response redirect?

Response. Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister. aspx" page and it has a button that redirects you to the "UserDetail.

What can I use instead of a response redirect?

For instance, instead of a "form" button that causes a postback and redirect, you could use a LinkButton that will behave like a hyperlink, allowing the browser to request the new page directly.

Does response redirect stop execution?

Response. Redirect("Default. aspx", true) means current page execution is terminated and page is redirected to the default.


2 Answers

You can't is the short answer. The browser is the only thing that can open up a new window.

What you can do is send a chunk of html down the response that has a link with your url as an href, target="_blank" and a chunk of javascript onload of the form that fakes a click. If this doesn't work then use a window.open(url);

response.write("<script>");
response.write("window.open('page.html','_blank')");
response.write("</script>"); 
like image 56
Dave Walker Avatar answered Sep 28 '22 19:09

Dave Walker


You're trying to accomplish a client-side task from the server side, so you'll need to do a bit of hacking.

One option is sending back a page that's just a bit of JavaScript, which then will handle the redirect.

This isn't particularly clean, but what about:

Response.Write("<script>window.open('http://www.somesite.com/','_blank');</script>");
like image 39
Josh Earl Avatar answered Sep 28 '22 21:09

Josh Earl