Javascript $.cookie 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4034190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:13:07  来源:igfitidea点击:

$.cookie is not a function

javascriptjquery

提问by Introgy

When I try to load my page that uses jquery, when the following line is hit:

当我尝试加载使用 jquery 的页面时,当遇到以下行时:

if ($.cookie('compare') != null) 

I get the error $.cookie is not a function. Has anybody seen this before?

我收到错误 $.cookie is not a function。有没有人见过这个?

回答by Nick Craver

That means that the $.cookiepluginisn't being included in the page, at least not being it's getting called. Make sure it's both being included, and is being included beforeit's getting used. Include it just after jQuery itself to be safe.

这意味着该$.cookie插件没有包含在页面中,至少没有被调用。确保它被包括在内,并且在被使用之前被包括在内。将它包含在 jQuery 本身之后是安全的。

Just a tip: Several otherplugins rely on the cookie plugin (but don't necessarily check if it exists before calling it), you could be using one.

只是一个提示:其他几个插件依赖于 cookie 插件(但在调用它之前不一定要检查它是否存在),您可能正在使用一个。

回答by Klaus Byskov Pedersen

Do you have the jQuery cookie plugin?

你有jQuery cookie 插件吗?

回答by SpYk3HH

No but I can show you how to make one very easy like ...

不,但我可以向您展示如何使一个非常简单,例如...

Create a seperate js file, name it what you want, for ease of this, i'll call it jCook.

创建一个单独的 js 文件,将其命名为您想要的名称,为了方便起见,我将其称为 jCook。

In your header, after you add jQuery, add your new file:

在标题中,添加 jQuery 后,添加新文件:

<script type="text/javascript" src="jquery.js"></script>  
<script type="text/javascript" src="jCook.js"></script>  <!-- here it is! -->

And below is the EASY code to put in the file:

下面是放在文件中的简单代码:

(function($) {
    if (!$.setCookie) {
        $.extend({
            setCookie: function(c_name, value, exdays) {
                try {
                    if (!c_name) return false;
                    var exdate = new Date();
                    exdate.setDate(exdate.getDate() + exdays);
                    var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
                    document.cookie = c_name + "=" + c_value;
                }
                catch(err) {
                    return false;
                };
                return true;
            }
        });
    };
    if (!$.getCookie) {
        $.extend({
            getCookie: function(c_name) {
                try {
                    var i, x, y,
                        ARRcookies = document.cookie.split(";");
                    for (i = 0; i < ARRcookies.length; i++) {
                        x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
                        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
                        x = x.replace(/^\s+|\s+$/g,"");
                        if (x == c_name) return unescape(y);
                    };
                }
                catch(err) {
                    return false;
                };
                return false;
            }
        });
    };
})(jQuery);

The try catch and what not just helps to ensure you get a "false" return if you do somthing wrong, but really shouldn't ever be an issue. To use the code is easy...

如果你做错了什么,try catch 和什么不仅有助于确保你得到“错误”的回报,而且真的不应该成为一个问题。使用代码很容易...

On your page, where your load code or whatever is just do the following:

在您的页面上,您的加载代码或其他内容只需执行以下操作:

$.setCookie("nameOfCookie", "someValue", 30); // where 30 is the number 
// of days to expire, or you could leave blank as:
$.setCookie("nameOfCookie", "someValue")

// And to retrieve your cookie
$.getCookie("nameOfCookie");

See how simple that was!?

看看有多简单!?

And just to resolve, below is a real world example use to save a state of a select drop down menu

只是为了解决,下面是一个真实世界的例子,用于保存选择下拉菜单的状态

$(function(){
        $("select[name=somthing]").change(function(e) {
            $.setCookie("selectThis", $(this).val());
        });
        //  And to use ...
        if ($.getCookie("selectThis")) $("select[name=somthing]").val( $.getCookie("selectThis") ).change();
});

回答by Syed Ehtsham Abbas

Basically there could be various reasons for this problem.

基本上,这个问题可能有多种原因。

  1. You might not have added jquery plugin.
  2. Make sure to include jQuery file before the cookie plugin
  3. If you already have done above two then try downloading the new version of jquery-cookie pluginthat also solved my problem.
  1. 您可能没有添加 jquery 插件。
  2. 确保在 cookie 插件之前包含 jQuery 文件
  3. 如果您已经完成了以上两个操作,请尝试下载新版本的jquery-cookie 插件,该插件也解决了我的问题。

回答by Regan Rajan

Try enabling Mod_Sec for your domain, or asking your hosting company to enable it. Hope this helps.

尝试为您的域启用 Mod_Sec,或要求您的托管公司启用它。希望这可以帮助。

回答by BlackShift

The same problem occured in our Drupal 7 installation. It was caused, if I understand correctly, by ModSecurity configuration in cPanel blocking the file. See http://forums.cpanel.net/f5/mod-security-blocking-jquery-cookie-javascript-drupal-installation-191002.html

在我们的 Drupal 7 安装中也出现了同样的问题。如果我理解正确,这是由 cPanel 中的 ModSecurity 配置阻止文件引起的。见http://forums.cpanel.net/f5/mod-security-blocking-jquery-cookie-javascript-drupal-installation-191002.html

回答by HADI

I've been using this code -

我一直在使用这个代码 -

if( $.cookie('siteLayout') != undefined ){
   if( $.cookie('siteLayout') == 'compact' ) 
   $(settings.bodyElem).addClass('container');
}

And it works fine. But i was in-need to use another jQuery version in a specific page where version was 1.7.2 but i was actually using 1.10.2 After use multiple jQuery version i got this error -

它工作正常。但是我需要在版本为 1.7.2 的特定页面中使用另一个 jQuery 版本,但我实际上使用的是 1.10.2 使用多个 jQuery 版本后,我收到此错误 -

TypeError: $.cookie is not a function

类型错误:$.cookie 不是函数

then i used jQuery.cookieinstead of $.cookieand it works. Hope, this will also help ;)

然后我用jQuery.cookie而不是$.cookie它的工作原理。希望,这也会有所帮助;)

回答by Vani

For me the issue was fixed after downloading the latest version of jquery.cookie js

对我来说,下载最新版本的 jquery.cookie js 后问题已解决

回答by sinda

<script src="js/jquery.mobile-1.4.5.min.js"></script>
<script src="js/jquery.cookie.js"></script>

the first plugin must be juqery.js and the second plugin is cookie.js

第一个插件必须是 juqery.js,第二个插件是 cookie.js