-
-
Save allybee/5871749 to your computer and use it in GitHub Desktop.
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 | |
} | |
} | |
}; |
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
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.
How can I implement this script to my WordPress Website?
Thank you!
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.
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]]|