简体   繁体   English

如何检查jQuery中是否设置了PHP Cookie

[英]how to check a php cookie is set or not in jquery

I am developing w php web site. 我正在开发w php网站。 Here I have set a php cookie 在这里我设置了一个PHP cookie

 setcookie('hotelId',$resHotel); 

I want to check whether it's set or not using jquery or javascript. 我要检查它是否是设置或不使用jQuery或JavaScript。 I have used the following code 我使用了以下代码

if($.cookie('hotelId')) {
        alert("yes");
    };

But it's not working 但这没用

Is there is any way to find PHP cookie is exist or not in jquery 有没有什么办法可以找到PHP的cookie在jQuery的存在或不

Thanks 谢谢

That should work if these two conditions are met: 如果满足以下两个条件,则该方法应该起作用:

  • you have installed the extra library that you need to use $.cookie() 您已经安装了使用$.cookie()所需的额外库
  • you have called setcookie() before outputting any part of your html 您已经在输出html的任何部分之前调用了setcookie()

Are you sure that you have the right libraries loaded into the page? 您确定页面中已加载了正确的库吗? I don't think cookie behavior is included with jQuery by default. 我不认为默认情况下jQuery包含cookie行为。

You can view cookies manually easily. 您可以轻松地手动查看Cookie。 The method varies a little from browser to browser, but (in Firefox, for example) if you go to Preferences > Privacy > remove individual cookies, you can browse through them and see their contents. 每种浏览器的方法有所不同,但是(例如,在Firefox中)如果转到“偏好设置”>“隐私”>“删除单个Cookie”,则可以浏览它们并查看其内容。 This can help you debug whether your php code is setting the cookie as desired. 这可以帮助您调试您的php代码是否根据需要设置了cookie。

First get the specified cookie as follows 首先获取指定的cookie,如下

var cookie=$.cookie("set");
if(!cookie)
{
   $.cookie("set","foo"); //if there is no cookie set it
}

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

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