javascript - $.cookie 给出错误 : Uncaught TypeError

标签 javascript jquery

我正在写一个js文件

    checkCookiesAccepted();

    function checkCookiesAccepted() {
        if (!$.cookie("acecptcookies")) {
            showCookieBar();
            attachPageChangedEvents();
        }
    }


function attachPageChangedEvents(){
            // get all internal a hrefs and override onclick event so we can record acceptance
            var siteURL = "http://" + top.location.host.toString();

            //$("a[href^='"+siteURL+"'], a[href^='/'], a[href^='./'], a[href^='../'], a[href^='#']").click(acceptCookies);
            $("#middle a").click(acceptCookies);
        }
function acceptCookies(){
            $.cookie("acecptcookies", "1", { path: '/', expires: 20*365 });

        }
        function showCookieBar(){
            // create div elements to body element unless another is supplied
            $("<div id='tscookiebar'><div>This site uses cookies. To find out more about the cookies this site uses and how to manage them, please review the cookies section of our <a href='http://www.myproduct.co.uk/privacy_policy/PrivacyPolicy.pdf' target='_blank'>Privacy Policy</a>. By using our website, you agree that we can place these types of cookies on your device.</div></div>").prependTo("body");
        }
        $.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 || value === undefined)) {
                        options = $.extend({}, options);

                        if (value === null || value === undefined) {
                            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 || {};
                    var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

                    var pairs = document.cookie.split('; ');
                    for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
                        if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
                    }
                    return null;
                };

我还包括 jquery.min.js 和 jquery.cookie.js 但仍然给出错误 未捕获的类型错误:对象函数 (e,t){return new x.fn.init(e,t,r)} 没有方法 'cookie'

最佳答案

giving an error Uncaught TypeError: Object function (e,t){return new x.fn.init(e,t,r)} has no method 'cookie'

好的,这告诉我们您确实加载了 jQuery(因为这就是缩小的 jQuery 函数的样子)并且 jQuery 正在使用 $符号,但由于某种原因,当您运行该代码时,jQuery 函数上不存在 cookie 插件。可能的原因:

  1. 您的 Cookie 插件路径不正确,您将收到 404 错误消息。

  2. 您在加载 Cookie 插件之后加载 jQuery,无论是第一次(jquery.min.jsjquery.cookie.js 之后),或者您不小心加载了它>第二次,覆盖第一次。

  3. 您正在加载 jQuery 之后加载 Cookie 插件之前运行代码。

  4. 您有 script按正确的顺序标记标签,按您使用过的 the async attribute ,因此它们是无序执行的。

  5. 您正在添加 script使用代码(而不是标记)的元素。当您使用代码而不是标记添加脚本时,无法保证它们的执行顺序。

如果你有这个:

<script src="/path/jquery.min.js"></script>
<script src="/path/jquery.cookie.js"></script>
<script src="/path/your.script.js"></script>

...并且您没有收到任何 404 错误,它应该可以工作。

关于javascript - $.cookie 给出错误 : Uncaught TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19634016/

相关文章:

javascript - 为什么 jQuery 将冒号放在关键字之前?

javascript - 在不同的函数上访问 JQuery/AJAX 的返回值

jquery - 您可以同时传递序列化的 ajax 数据和其他变量吗?

javascript - 定义 TypeScript 调试变量

javascript - 这个网页的滚动效果是怎么实现的呢?

javascript - 我应该在 Promise.all 中使用 await 吗?

jquery - 使用 JQuery 在附加行中显示表详细信息

javascript - 在 Rails 应用程序中未找到来自 node_modules 的 .map 扩展名

javascript - 如何使用 JSON 填充完整日历?

jquery - Highcharts:如何更改图表类型