mercredi 27 novembre 2019

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.

Aucun commentaire:

Enregistrer un commentaire