简体   繁体   中英

Open page in a new tab from C#

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.

  1. You can have it open the URL with an anchor click, like so <a href="<url>" target="_blank">click me</a>
  2. You can submit a form to a blank target, giving a similar result, but from a form. This is useful if you need to post, but can also be useful for get. Like so <form method="get" action="<your destination>" target="_blank"><button type="submit">Click Me</button></form>
  3. You can use JS with 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM