0

I've searched for the answer to this question and only found half solutions to my issue.

I'm trying to write a cookie based on an URL parameter, and then hide or show page content based on if that cookie is present.

I'm trying to use js-cookie.

If someone comes to the site with the parameter ?ent=a1 attached to their url, a cookie would be written called ent_afil with the value of one. If the ent_afil cookie is present a div with the ID default-content is hidden and a div with the ID a1 is shown. If the URL parameter isn't present, no cookie is written and the default-content div remains.

function SetCookie() {
var url = window.location.search;
if(url.indexOf('?ent=a1') !== -1)
    Cookies.set('ent_afil', 'one', { expires: 5 });
}
if (Cookies.get("ent_afil") == 'one')
{
    $("#default-content").hide();
    $("#a1").show();

}
else {
    $("#default-content").show();
    $("#a1").hide();
}
Community
  • 1
  • 1
  • Go check your browser console and let it tell you about the error in your script. – CBroe Feb 24 '17 at 19:44
  • Good point. I updated the script I'm not getting and console errors, but it is still not functioning. – checkmanent Feb 24 '17 at 19:56
  • `if (Cookies.get("entAfil") == 'a1')` - is that your actual current code, or did someone else mess this up while editing? (The value written into the cookie is "one", not "a1".) – CBroe Feb 24 '17 at 20:02
  • Something was off when editing. Its updated, however, the cookie isn't being written. – checkmanent Feb 24 '17 at 20:09
  • _"however, the cookie isn't being written"_ - how exactly did you check that? Add a concole.log debug statement or an alert to verify if it actually goes into the first if-branch. – CBroe Feb 24 '17 at 20:34
  • Nothing shows up in the inspector under storage/ cookies. But if I take 'Cookies.set('ent_afil', 'one', { expires: 5 });' out of the function and use it without alone, ent_afil one does show up in storage/cookies. – checkmanent Feb 24 '17 at 20:45
  • 1
    But you are actually calling that `SetCookie` function somewhere, right? – CBroe Feb 24 '17 at 20:48
  • No. Thats my entire script. – checkmanent Feb 24 '17 at 20:55
  • Well functions don't call themselves ... (and don't anyone dare say IIFE now ;-) Why is that inside a function in the first place? The part of your code that _is_ inside the function doesn't need to be in one, you can just execute those lines directly, at any time. Depending on where you embed the script however, the parts that are outside the function (so everything starting with the second if) might need to be called later, because the DOM elements they want to select need to exist at that point already. (But if you place the script at the very end of the body that is not an issue either.) – CBroe Feb 24 '17 at 21:00

1 Answers1

0

Removed the function like CBroe suggested, and it works now.

var url = window.location.search;
if(url.indexOf('?ent=a1') !== -1)
    Cookies.set('ent_afil', 'one', { expires: 5 });

if (Cookies.get("ent_afil") == 'one')
{
    $("#default-content").hide();
    $("#a1").show();

}
 else {
    $("#default-content").show();
    $("#a1").hide();
}