【问题标题】:Cookie Script - Uncaught ReferenceError: e is not definedCookie 脚本 - 未捕获的 ReferenceError:未定义 e
【发布时间】:2016-08-23 09:12:09
【问题描述】:

我正在编写一个欧盟 cookie 脚本,但似乎无法修复它。错误如下:

  • 未捕获的 ReferenceError:未定义 e

我在 WordPress 上运行脚本。在加载此脚本之前,jQuery 已经存在。

脚本:

jQuery(document).ready(function($){
    e.CookiesMessage = function(o) { // this line creates the error
        function n(o) {
            var i = "";
            1 == o.closeEnable && (i += '<a href="#" id="optr-cookies-close" style="background-color:' + o.closeBgColor + ';"><svg version="1.1" id="optr-cookies-close-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="248.5 248.5 15 15" enable-background="new 248.5 248.5 15 15" xml:space="preserve" ><polygon id="x-mark-icon" points="263.5,260.876 258.621,255.999 263.499,251.121 260.876,248.5 256,253.377 251.122,248.5 248.5,251.121 253.378,255.999 248.5,260.878 251.121,263.5 256,258.62 260.879,263.499" style="fill:' + o.closeColor + ';"/></svg></a>');
            var n = "";
            1 == o.acceptEnable && (n += '<a href="#" id="optr-cookies-ok">' + o.acceptText + "</a>"), 1 == o.infoEnable && (n += '<a href="' + o.infoUrl + '" id="optr-cookies-info">' + o.infoText + "</a>");
            var c = '<div id="optr-cookies"><p>' + o.messageText + n + "</p>" + i + "</div>";
            e("body").prepend(c), e("#optr-cookies").hide().slideDown(), e("#optr-cookies").css({
                "background-color": o.messageBg,
                color: o.messageColor
            }), e("#optr-cookies p a").css({
                color: o.messageLinkColor
            })
        }

        function c(e) {
            return e.replace(/^\s+|\s+$/g, "")
        }

        function s(e) {
            var o = !1;
            if (document.cookie) {
                var n = document.cookie.split(";");
                for (i = 0; i < n.length; i++) {
                    var s = n[i].split("=");
                    c(s[0]) == e && (o = s[1])
                }
            }
            return o
        }

        function a(e, o, i, n) {
            var c = new Date;
            c.setTime(c.getTime() + 24 * i * 60 * 60 * 1e3);
            var s = "expires=" + c.toUTCString();
            document.cookie = e + "=" + o + "; " + s + "; path=" + n + ";"
        }
        var l = {
            messageText: "We use technical and analytics cookies to ensure that we give you the best experience on our website.",
            messageBg: "#151515",
            messageColor: "#FFFFFF",
            messageLinkColor: "#F0FFAA",
            closeEnable: !0,
            closeColor: "#444444",
            closeBgColor: "#000000",
            acceptEnable: !0,
            acceptText: "Accept & Close",
            infoEnable: !0,
            infoText: "More Info",
            infoUrl: "#",
            cookieExpire: 180
        };
        o = e.extend(l, o);
        var t = location.host,
            r = "Cookies policy accepted",
            d = "/",
            g = s(t);
        g || n(o), e("#optr-cookies-ok").on("click", function(i) {
            i.preventDefault(), a(t, r, o.cookieExpire, d), e("#optr-cookies").slideToggle()
        }), e("#optr-cookies-close").on("click", function(o) {
            o.preventDefault(), e("#optr-cookies").slideToggle()
        })
    }
});

我真的希望有人可以帮助我解决这个问题。谢谢!

我从this website获取脚本。


编辑:修复上述问题后出现此错误。

未捕获的类型错误:$.CookiesMessage 不是函数

导致这一行:

jQuery(document).ready(function($){
    $.CookiesMessage({ // this one
        messageText: "We use technical and analytics cookies to ensure that we give you the best experience on our website.",
        messageBg: "#151515",
        messageColor: "#FFFFFF",
        messageLinkColor: "#F0FFAA",
        closeEnable: true,
        closeColor: "#444444",
        closeBgColor: "#000000",
        acceptEnable: true,
        acceptText: "Accept & Close",
        infoEnable: true,
        infoText: "More Info",
        infoUrl: "#",
        cookieExpire: 180
    });
});

<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#" class="no-js">
    <head>  <meta charset="UTF-8">
    <meta name="description" content="">

<script type='text/javascript' src='http://localhost/wordpresstest/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>
<script type='text/javascript' src='http://localhost/wordpresstest/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>
<script type='text/javascript' src='http://localhost/wordpresstest/wp-content/plugins/cookiez*emphasized text*/js/cookies.js?ver=1.0.0'></script>

<style type="text/css">#optr-cookies { position: fixed; bottom: 0; left: 0; z-index: 200; width: 100%; padding: 15px 0; text-align: center; font-size: 14px; line-height: 1.1; background-color: #151515; color: #FFF; box-shadow: 0 -3px 3px 0 rgba(0, 0, 0, .15) } #optr-cookies p { margin: 0; padding: 0 50px } #optr-cookies-info, #optr-cookies-ok { display: inline-block; color: #F0FFAA; font-weight: 700; text-decoration: underline; margin-left: 10px; cursor: pointer } #optr-cookies-close { height: 16px; width: 16px; padding: 8px; position: absolute; right: 7px; top: 50%; margin-top: -16px; -moz-border-radius: 16px; -webkit-border-radius: 16px; border-radius: 16px; background-color: #000 } @media (max-width: 768px) { #optr-cookies p { padding: 15px 15px 0 } #optr-cookies-info, #optr-cookies-ok { display: block; text-decoration: none; padding: 10px 5px; margin-top: 10px; background-color: #444; -moz-border-radius: 2px; -webkit-border-radius: 2px; border-radius: 2px } #optr-cookies-close { left: 50%; margin-left: -16px; top: 0 } }</style><script type="text/javascript">jQuery(document).ready(function($){
    $.CookieMessage = function(o) {};
});</script>
    </head>
    <body id="document" class="home blog logged-in one-column">
        <!-- website -->

    </body><!-- #document -->
</html>

【问题讨论】:

  • 这行中的e 变量是从哪里得到的?
  • 你从哪里得到这个代码?您提供的网站不同。
  • @Maxx 这是脚本:jqueryscript.net/demo/… - 我通过 JSBeautifier.org 运行它
  • 我明白了,但是你的脚本是做什么的?您是否只是将该文件的一部分复制粘贴到您的脚本中?或者你从哪里得到这个代码?
  • @Maxx 只需复制和粘贴,然后使其与 wordpress 兼容

标签: javascript jquery


【解决方案1】:

我想我明白你做了什么。

将整个 js 代码替换为

$(document).ready(function(){
    $.CookiesMessage({});
});

别忘了

<link href="dist/cookies-message.min.css" rel="stylesheet">
<script src="dist/cookies-message.min.js"></script>

jQuery 加载后

【讨论】:

  • 在 jquery 之后加载它,但我需要 &lt;head&gt; 中的脚本,以便调整设置
  • 你可以把它放在head里,就在jQuery和plugin之后。
  • 好的,我在第60行得到Uncaught SyntaxError: Unexpected token,第60行是这一行:messageBg: "#151515",
  • 在我的答案中尝试使用空对象,只需复制粘贴整个代码即可。成功了吗?
  • 我已经使用 Tim Vermaelen 提供的代码更改了您的代码,并且没有弹出错误,但是如何向其中添加设置代码?
【解决方案2】:

问题出在e.CookiesMessage。由于 e 是 undefined,因此您将无法将另一个 Object 绑定到其中。

试试这个:

var e = e || {};
e.CookiesMessage = function(o) { // this line creates the error

这很可能会解决您的问题。

现在我们都是检查员,e 很可能是 Event 处理程序的一部分,因此我们可以假设您从其他地方复制/粘贴了此代码。为了理解这一点,试试这个:

jQuery(document).ready(function($){
    $.CookieMessage = function(o) {
        // and the rest of the code
    }
});

【讨论】:

  • 成功了,谢谢。出现另一个错误,TypeError: $.CookiesMessage is not a function,位于 head 标签内,我将更新我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2016-11-03
  • 2011-01-05
  • 2016-01-02
  • 2013-10-06
  • 2016-12-17
相关资源
最近更新 更多