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

1 回答 1

0

删除了 CBroe 建议的功能,现在可以使用。

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();
}
于 2017-02-27T14:22:52.657 回答