thanby
thanby

Reputation: 323

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?

Upvotes: 1

Views: 2120

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 98861

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

Upvotes: 1

Marcos Pérez Gude
Marcos Pérez Gude

Reputation: 22158

In javascript:

   window.open("url");

Or adding the attr:

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

With the following html

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

Upvotes: 4

Related Questions