Skip to content

Instantly share code, notes, and snippets.

@allybee
Last active November 7, 2023 22:40
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save allybee/5871749 to your computer and use it in GitHub Desktop.
Save allybee/5871749 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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
Copy link

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
Copy link

How can I implement this script to my WordPress Website?

@j3ll3yfi5h
Copy link

Thank you!

@peterxavier01
Copy link

To add this code to your WordPress website, you can download a plugin to add JavaScript code. One of such plugins is the Simple Custom CSS and JS plugin by SilkyPress.com.

This plugin is awesome as it allows you to add script tags to either the header or footer. It also gives the option of linking the file either as an internal or external file. Last but not the least, it allows you to set the location in which the script tags affect.

I hope this is able to help anyone who finds this.

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