Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add target="_blank" to external links with pure JavaScript.
function targetBlank() {
// remove subdomain of current site's url and setup regex
var internal = location.host.replace("www.", "");
internal = new RegExp(internal, "i");
var a = document.getElementsByTagName('a'); // then, grab every link on the page
for (var i = 0; i < a.length; i++) {
var href = a[i].host; // set the host of each link
if( !internal.test(href) ) { // make sure the href doesn't contain current site's host
a[i].setAttribute('target', '_blank'); // if it doesn't, set attributes
}
}
};
@aka-sektor

This comment has been minimized.

Copy link

@aka-sektor aka-sektor commented Feb 2, 2017

Please tell me: where I need to add this code?

I tried add to MediaWiki: Common.js
It's not work with this wikicode: [[File:image_name.png|link=http://example.com]]|

@vastolf

This comment has been minimized.

Copy link

@vastolf vastolf commented May 24, 2019

Works great @allybee thanks for sharing!

@aka-sektor you need to run the function:

// Define the Function targetBlank()

function targetBlank() {
  // remove subdomain of current site's url and setup regex
  var internal = location.host.replace("www.", "");
      internal = new RegExp(internal, "i");
      
  var a = document.getElementsByTagName('a'); // then, grab every link on the page
  for (var i = 0; i < a.length; i++) {
    var href = a[i].host; // set the host of each link
    if( !internal.test(href) ) { // make sure the href doesn't contain current site's host
      a[i].setAttribute('target', '_blank'); // if it doesn't, set attributes
    }
  }
};

// Run the function targetBlank()

targetBlank();

Might make sense to re-name the function as targetBlank might conflict with some other library

@mukkaswan

This comment has been minimized.

Copy link

@mukkaswan mukkaswan commented May 9, 2020

Works great @allybee thanks for sharing!

@aka-sektor you need to run the function:

// Define the Function targetBlank()

function targetBlank() {
  // remove subdomain of current site's url and setup regex
  var internal = location.host.replace("www.", "");
      internal = new RegExp(internal, "i");
      
  var a = document.getElementsByTagName('a'); // then, grab every link on the page
  for (var i = 0; i < a.length; i++) {
    var href = a[i].host; // set the host of each link
    if( !internal.test(href) ) { // make sure the href doesn't contain current site's host
      a[i].setAttribute('target', '_blank'); // if it doesn't, set attributes
    }
  }
};

// Run the function targetBlank()

targetBlank();

Might make sense to re-name the function as targetBlank might conflict with some other library

Please suggest where to use the function targetBlank();
If it is to use as {a href="#" onclick="targetBlank();"} in every anchor tag on the page, then this code is of no means.

@kulterryan

This comment has been minimized.

Copy link

@kulterryan kulterryan commented Mar 19, 2021

How can I implement this script to my WordPress Website?

@j3ll3yfi5h

This comment has been minimized.

Copy link

@j3ll3yfi5h j3ll3yfi5h commented May 4, 2021

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment