Home ›
JavaScript to open external sites (not on domain or subdomain) in new targetJavaScript to open external sites (not on domain or subdomain) in new target
Submitted by Benjamin Melançon on February 4, 2013 - 12:04pm
in
It's bad Web practice to open any links in a separate window, but it does help when going for an HTML mobile app and it's becoming accepted expected behavior for anything with a stream.
So here's the JavaScript:
// Open external links in a new window or tab.
var domain = window.location.hostname;
domain = domain.substring(domain.indexOf('.') + 1);
var not = '[href*="' + domain + '"]';
$("a[href^='http']").not(not).attr('target','_blank');
From the commit log:
Get current domain, open non-current in new But ignore subdomains. This keeps people on Activore while browsing Activore but open non-Activore links in a new window. Note: As written it will also open "http://FAKEactivore.net" in the same window. This was considered preferable to opening "http://activore.net" in a new window, and i deemed further checking needlessly complicated. Fixes #1736 ... better, this time, as this works on test sites, which we really need.
Searched words:
JQuery make links to outside web sites use a new tab or window
JS get host
javascript truncate string
Comments
Post new comment