Does anybody know how to open an URL in a new tab from C# code? I have tried with
Response.Write("<script type='text/javascript'>window.location.href('../Documents/doc.pdf','_blank'); </script>");
Response.Write("<script type='text/javascript'>window.open('../Documents/doc.pdf','_blank'); </script>");
Response.Write("$('#pageContent_Send').click();");
with
$("#pageContent_Send").click(function () {
window.open("../Documents/doc.pdf");
return false;
});
and it did not work, using "window.open" I get an "Pop-up Blocker" browser warning.
There are a few options for opening a new tab that won't get blocked.
<a href="<url>" target="_blank">click me</a>
<form method="get" action="<your destination>" target="_blank"><button type="submit">Click Me</button></form>
window.open
but it has to be tied to an active click event. I see in your original post that this got blocked, but if you're triggering it on a click event directly (ie not using a setTimeout or an async call or something else that delays the window opening) then this should work in all browsers. Since you're trying to "fake" it by forcing a click, the browser is going to block this every time unless you explicitly allow it. <a href='www.xyz.com' target='_blank'>Click me to open me in new tab</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.