I’m trying to open all external links on a site in a new window. However on there are 2 versions of the site, e.g. a store and the main site. So on the main site we might have links that go to http://store.example.com for example.
I’ve got some code here which will allow me to open all the external links in a new window. However I’d like to be able to exclude certain domains. Like the one I’ve mentioned above.
Here is the code:
$(document).ready(function() {
$("a[href^=http]").each(function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr({
target: "_blank",
title: "Opens in a new window"
});
}
})
});
I’m new to JS / jQuery so any additional information would be brilliant.
For triggering the clicks programmatically, you can do something like: