Search code examples
jquerycookiesjquery-cookie

Setting a cookie from url


When someone goes onto the website with this on the end of the url: /?utm_source=google I am trying to set a cookie and add a class to the body

At the moment it doesnt seem to be working correctly and it adds the class onto the body if you just go straight to the homepage

I am using the jquery cookie plugin

Don't suppose if anyone knows what I am doing wrong please, or a better way of doing it?

  var url = "(location.href.match(/utm\_source\=google/ig))";

  $.cookie("url", url, { expires:7, path: '/' });

 if ($.cookie("url")) {
    $('body').addClass('test');
  } 

Solution

  • Nevermind i got it working like so:

    var url = "(location.href.match(/utm\_source\=google/ig))";
    
       if (location.href.match(/utm\_source\=google/ig)){
         $('body').addClass('test');
         $.cookie('number', 'url');
    
      }
    
        if ($.cookie('number')) {
             $('body').addClass('test');
        }