Make all external links open in a new tab except one!

2

Oops, I have a script that identifies all external links and forces open in new tab ...

<script>
 jQuery(document).ready(function($){
     $('a').not('[href*="'+document.domain+'"]').attr('target', '_blank');
     $('a').not('[href*="'+document.domain+'"]').attr('rel', 'external nofollow');
 });
 </script>

But how to make it not force a domain ..., ie .. all domains open in new tab .. less the one of "somesite.com" ?? that is .. only links from that domain will not open in a new tab.

I tried doing this:

 <script>
	 jQuery(document).ready(function($){
		 $('a').not('[href*="'+document.domain+'"]').attr('target', '_blank');
		 $('a').not('[href*="'+document.domain+'"]').attr('rel', 'external nofollow');
		 $('[href*="ads.onlinee.top"]').removeAttr("target");
	 });
 </script>

But the ad links .. (EXTERNAL LINKS BACK IN THE SAME GUIDE)

    
asked by anonymous 16.10.2018 / 22:40

1 answer

1

" "This "works " "

 " " "$("a[href^=http]").each(function(){

 " " " " " "// "domínio "excluído "(mesma "janela)
 " " " " " "var "excludes "= "[
 " " " " " " " " "'dominio.com'
 " " " " " " " " "];

 " " " " " " " " "if(this.href.indexOf(excludes) "!= "-1) "{
 " " " " " " " " " " " "return "true;
 " " " " " " " " "}

 " " " " " "if(this.href.indexOf(location.hostname) "== "-1) "{

 " " " " " " " " " " "$(this).click(function() "{ "return "true; "}); "

 " " " " " " " " " " "$(this).attr({
 " " " " " " " " " " " " " " "target: ""_blank"
 " " " " " " " " " " "});

 " " " " " " " " " " "$(this).click();
 " " " " " "}
 " " "})
});

"If "you "want "to "insert "a "list: "

$(document).ready(function() "{

 " " "$("a[href^=http]").each(function(){

 " " " " " "// "NEW "- "excluded "domains "list
 " " " " " "var "excludes "= "[
 " " " " " " " " "'excludeddomain1.com',
 " " " " " " " " "'excludeddomain2.com',
 " " " " " " " " "'excluded.subdomain.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",
 " " " " " " " " " " " " " " "title: ""Opens "in "a "new "window"
 " " " " " " " " " " "});

 " " " " " " " " " " "$(this).click(); "// "trigger "it
 " " " " " "}
 " " "})
});

"
16.10.2018 / 23:39