简体   繁体   English

如何使用jquery.cookie仅将cookie设置为主页而不是其他页面

[英]How to set cookie for homepage only and not other pages using jquery.cookie

jQuery(window).scroll(function(){
    if(jQuery("a.down-arrow-wrap").hasClass("fade-out-down-arrow")){
        jQuery.cookie('hide-down-arrow', true, { path: '/' });
    }
});

jQuery(function () {
    if (!jQuery.cookie('hide-down-arrow')) {
        jQuery("a.down-arrow-wrap").css({"display":"block"});
    }
});

In above code, I am setting cookie on page scroll. 在上面的代码中,我在页面滚动上设置cookie。 and on second part checking if that cookie does not exist than display <a> with class hide-down-arrow . 然后在第二部分中检查该cookie是否不存在,然后使用class hide-down-arrow显示<a>

I want this to happen only on homepage and not on any other pages. 我希望这种情况仅发生在首页上,而不发生在其他任何页面上。

Thanks. 谢谢。

Here is an easy way, you can check the page name before display or before check existing cookie, 这是一种简单的方法,您可以在显示之前或在检查现有Cookie之前检查页面名称,

try this: 尝试这个:

var pathName = window.location.pathname;
var vars = query.split("/"); 
if ((vars[vars.length - 1]).toLowerCase() == "homepage.html")
   //if the cookie is exist then display <a>

You can use path option from jquery plugin to explicitly set cookie validity to particular page. 您可以使用jquery插件中的path选项将cookie有效性显式设置为特定页面。

jquery-cookie path jQuery Cookie路径

Define the path where the cookie is valid. 定义cookie有效的路径。 By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). 默认情况下,cookie的路径是创建cookie的页面的路径(标准浏览器行为)。 If you want to make it available for instance across the entire domain use path: '/'. 如果要使其在整个域中例如可用,请使用路径:“ /”。 Default: path of page where the cookie was created. 默认值:创建cookie的页面路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM