Tom tom
Tom tom

Reputation: 331

Adding a if statement

My code looks like this:

$('a').each(function(){
  $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source  + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
});

The code adds some UTM Parameters to all Links on a site. But now I want to exclude some specific links:

https://www.mysite.or/cat1
https://www.mysite.or/cat2

So I came up with that:

$('a').each(function() {
  if ("href:contains('https://www.mysite.or/cat1')") {
    $(this).attr('href');
  } else if ("href:contains('https://www.mysite.or/cat1')") {
    $(this).attr('href');
  } else {
    $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
  }
});

But it isn't working properly.

Upvotes: 0

Views: 112

Answers (3)

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

It will be better if you create a list of excluded domains so it will be easier to add more in the future. For example like this:

var exclude = [
  "https://www.mysite.or/cat1",
  "https://www.anysite.com"
]

$('a').each(function() {
  var href = $(this).attr('href');
  if (exclude.indexOf(href) === -1) {
    $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
  }
});

As @caramba pointed out in the comments, the above solution is not the fastest if you care about performance (i.e you have thousands of a to iterate over, or tens of excluded domains). Another possible solution (while keeping the code easy to maintain) is to build a selector and pass it to jQuery:

var exclude = [
  "https://www.mysite.or/cat1",
  "https://www.anysite.com"
]

var nots = exclude.map(function(domain) {
    return ':not([href="' + domain + '"])';
}).join('');

$('a' + nots).each(function() {
  var href = $(this).attr('href');
  $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium);
});

Upvotes: 0

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123377

Exclude all the url not required by reducing the scope of the selector

$('a:not([href$="cat1"]):not([href$="cat2"])').each(function() {
  $(this).attr('href', ...);
});

$= means attribute ending with

 $('a:not([href$="cat1"]):not([href$="cat2"])').each(function() {
     $(this).html('changed')
  });    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<a href="http://somelink/cat0">untouched</a>
<a href="http://somelink/cat1">untouched</a>
<a href="http://somelink/cat2">untouched</a>
<a href="http://somelink/cat3">untouched</a>
<a href="http://somelink/cat4">untouched</a>

Upvotes: 1

caramba
caramba

Reputation: 22480

you could also do something like:

$('a:not([href^="https://www.mysite.or/cat"])')

which will only select <a> tags not containing a href which starts with https://www.mysite.or/cat

$('a:not([href^="https://www.mysite.or/cat"])').each(function() {
    $(this).addClass('found');
});
.found {
    background-color: orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#">some</a>
<a href="https://www.mysite.or/cat2">xxx1</a>
<a href="https://www.mysite.or/cat1">xxx1</a>

Upvotes: 1

Related Questions