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

Asked

Viewed 98 times

1

Oops, I have a script that identifies all external links and force 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..., that is.. all domains open in a new tab.. except that of "algumsite.com"?? that is.. only links from this domain will not open in new tab.

I tried to do so:

 <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 OPEN AGAIN IN THE SAME TAB)

  • I believe with regex you can do this

  • I think if I add one more .not Voce achieves the result. $('a').not('[href*="'+document.domain+'"]').not('[href*="algumsite.com"]').attr('target', '_blank');

  • this someone.com has to open on the same page!

  • I tried that one, it didn’t work!

  • All of the same domain or ["some.com", "othotambem.com"] must open on the same page?

  • All of the domain "site01.com" opens in the same tab... other external domains.. open in new tab..

  • In the code I put up he was putting target for the elements to whose href didn’t have the current domain and someone with.. It may be that your link already has the target='_blank', then Voce needs to remove the target. You can select it and remove with the following code $('[href*="algumsite.com"]').removeAttr("target"). jQuery Selectors

  • I put another one... but it stopped working the others..

  • I put in question my attempt... added your code..

  • Also add html, as declared <a href="">

  • what do you mean ? how do I put it ?

Show 6 more comments

2 answers

1

This one 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
      }
   })
});

Source - techfoobar - Soen

0


I used this:

$(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
      }
   })
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.