简体   繁体   中英

Force a URL to open in a new tab

I have a webpart on a site that allows you to insert a "Show more" link to an external page that expands on the information displayed in the webpart. Unfortunately this option only takes a regular URL as the value for the link, it doesn't let you construct the HTML link itself. I need this link to open in a new tab but since I only get to put the URL in, I can't use the normal target="_blank" HTML code. Is there a way to craft the URL itself to force a new tab?

In javascript:

   window.open("url");

Or adding the attr:

   document.getElementById("theLink").setAttribute("target", "_blank");

With the following html

   <a id="theLink" href="url">

If you cannot modify any part the a tag, you can use jquery .
The following script will try to open all links on a different tab/window:

$("a").on("click",function(){
       event.preventDefault();
       window.open($(this).attr('href'),'_blank');
});

NOTE:

Make sure you read this answer

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