My website have tons of internal and external URL.
for my hostname or "#" href
it should open in same tab but for any other domain then my hostname should be open in new tab.
This code making all url open in new tab including internal links.
<base target="_blank" rel="noopener noreferrer">
Also tried (https://stackoverflow.com/a/12071371)
$(document).ready(function() {
$('a[href^="https://"],a[href^="http://"]').each(function(){
// NEW - excluded domains list
var excludes = [
'google.com',
'www.example.com',
'*.example.com'
];
for(i=0; i<excludes.length; i++) {
if(this.href.indexOf(excludes[i]) != -1) {
return true; // continue each() with next link
}
}
if(this.href.indexOf(location.hostname) == -1) {
// attach a do-nothing event handler to ensure we can 'trigger' a click on this link
$(this).click(function() { return true; });
$(this).attr({
target: "_blank",
rel: "noreferrer nofollow noopener",
title: "Opens in a new window"
});
$(this).click(); // trigger it
}
})
});
Please provide the solution in HTML, javascript or PHP.
base
tag, usetarget="_self"
on links that should override that. Or, if there are more links that should open in the current tab, don't usebase
at all and just addtarget="_blank"
to the links that should be external.target="_self"
ortarget="_blank"
for all of them<a>
s href attribue. If it doesn't contain your domain name or is not#
, addtarget="_blank"
to the<a>