[ACCEPTED]-How to automatically add target="_blank" to external links only?-django
I've been using the following for awhile. Can't 10 remember where I found it originally:
$.expr[':'].external = function(obj){
return !obj.href.match(/^mailto\:/)
&& (obj.hostname != location.hostname)
&& !obj.href.match(/^javascript\:/)
&& !obj.href.match(/^$/)
};
That 9 adds an :external
jQuery selector, so then you can 8 just do:
$('a:external').attr('target', '_blank');
The nice part about using the custom 7 selector, is that if you need to modify 6 what contitutes an "external" link, you 5 can change it in one place and not worry 4 about the rest of your code. For instance 3 in my organization, we have certain subdomains 2 that aren't "external", but that we still 1 want to open in new windows.
Try something like
for (var links = document.links, i = 0, a; a = links[i]; i++) {
if (a.host !== location.host) {
a.target = '_blank';
}
}
Don't forget to run the 2 script by the time all links exist in the 1 document tree - in window.onload
event.
You could do something like this:
$(document.body).on('mouseover', 'a[target!=_blank]:not(.local)', function (evt) {
var a = $(this);
var href = a.attr('href');
var domain = href.match(/^https?:\/\/([^:\/]+)/);
if (domain && domain[1] && domain[1] !== "yourdomain.com") {
a.attr('target', '_blank');
} else {
a.addClass('local');
}
});
This will 8 process each link as you click it, and shouldn't 7 process each link more than once. If it 6 needs to be external, the target
will be set to 5 _blank
and it should open in a new window. Here's a working jsfiddle.
Update: My 4 method of determining if the link stays 3 on-site or not is quite crude. The method 2 in this answer is more thorough. I would probably replace 1 my simple regex match with that test instead.
I recommend you do that server side. Modify 2 the template of the page depending on the 1 locality of the link.
As the great accepted answer from @Chris 3 Pratt does not work for e.g. tel: links 2 and other special cases I just use the following 1 variant in order to not touch special links:
(function($) {
$.expr[':'].external = function(obj){
return (obj.hostname != location.hostname) && obj.href.startsWith("http");
};
$('a:external').attr('target', '_blank');
}) (jQuery);
You could also do this:
$("a[href^='http://']").attr("target","_blank");
or
$('a').each(function() {
var a = new RegExp('/' + window.location.host + '/');
if(!a.test(this.href)) {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
});
}
});
0
Slight change in code, which doesn't give 1 errors, additional = in !==
$.expr[':'].external = function(obj){
return !obj.href.match(/^mailto\:/) && (obj.hostname !== location.hostname) && !obj.href.match(/^javascript\:/) && !obj.href.match(/^$/);
};
$('a:external').attr('target', '_blank');
Another JavaScript solution:
(() => {
(document.querySelectorAll('a')).forEach(link => {
link.hostname !== location.hostname && link.setAttribute('target', '_blank');
})
})();
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.