javascript 表达式 '$.cookie' [undefined] 的结果不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11558319/
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-10-26 13:40:04  来源:igfitidea点击:

Result of expression '$.cookie' [undefined] is not a function

javascriptjqueryjquery-uijquery-mobilecordova

提问by PPD

Hi all in my mobile application to pass variable in between various pages without ajax I am using cookie. I am setting cookie like :

大家好,在我的移动应用程序中,在没有 ajax 的情况下在不同页面之间传递变量我正在使用 cookie。我正在设置 cookie,如:

 $.cookie("userName", userName, { path: '/' });

in js file and in in another html file I am accessing it as:

在 js 文件和另一个 html 文件中,我正在访问它:

$.cookie('userName');

But I got error as:

但我得到的错误是:

Result of expression '$.cookie' [undefined] is not a function

I have one js file for cookie as: jquery.cookie.jsit contains following code:

我有一个用于 cookie 的 js 文件:jquery.cookie.js它包含以下代码:

/*jshint eqnull:true */
/*!
* jQuery Cookie Plugin v1.1
* https://github.com/carhartl/jquery-cookie
*
 * Copyright 2011, Klaus Hartl
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/GPL-2.0
 */
(function($, document) {

var pluses = /\+/g;
function raw(s) {
    return s;
}
function decoded(s) {
    return decodeURIComponent(s.replace(pluses, ' '));
}

$.cookie = function(key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value == null)) {
        options = $.extend({}, $.cookie.defaults, options);

        if (value == null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path    ? '; path=' + options.path : '',
            options.domain  ? '; domain=' + options.domain : '',
            options.secure  ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || $.cookie.defaults || {};
    var decode = options.raw ? raw : decoded;
    var cookies = document.cookie.split('; ');
    for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
        if (decode(parts.shift()) === key) {
            return decode(parts.join('='));
        }
    }
    return null;
};

  $.cookie.defaults = {};

   })(jQuery, document);

I had imported above js file in my html file also. I had also tried with setCookie('userName', 'userName', 1);why is it so? Any suggestion will be appreciated.

我也在我的 html 文件中导入了上面的 js 文件。我也试过setCookie('userName', 'userName', 1);为什么会这样?任何建议将不胜感激。

回答by Edd Morgan

Make sure your jquery.cookie.jsis actually included, and that it's included beforeyou call $.cookie().

确保您jquery.cookie.js的实际包含在内,并且调用之前已包含在内$.cookie()

回答by Trinh Hoang Nhu

Check if you have imported jquery.cookie.js correctly and try to access jquery function when they're ready

检查您是否已正确导入 jquery.cookie.js 并在它们准备好后尝试访问 jquery 功能

$(function(){

$.cookie('userName');

})

or

或者

$(document.ready(function(){
$.cookie('userName');

}));

回答by rogal111

Check in your console If jquery.cookie.jsis loaded without errors.

检查您的控制台如果jquery.cookie.js加载没有错误。

You can find information about opening the console in your browser in this Webmasters.SE question.

您可以在此 Webmasters.SE 问题中找到有关在浏览器中打开控制台的信息。

回答by Mohammadreza

I had same problem. make sure didn't call jquery-1.9.0.min.js lib againafterjquery.cookie.js.

我有同样的问题。确保在 jquery.cookie.js之后没有再次调用 jquery-1.9.0.min.js lib

web pages usually have master and detail page. maybe you refrence to Jquery library again in detail page.

网页通常有主页面和详细页面。也许您在详细信息页面中再次参考了 Jquery 库。

回答by Shujaath Khan

 if (typeof $.cookie("user-popup") == "undefined") {}

you can check with this to check Cookie value is available or not

您可以使用此检查以检查 Cookie 值是否可用