Cookie脚本 - 未捕获的ReferenceError:未定义e

时间:2016-08-23 09:12:09

标签: javascript jquery

我正在制作欧盟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获取了剧本。

编辑:修复上述内容后出现此错误。

  

未捕获的TypeError:$ .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>

2 个答案:

答案 0 :(得分:1)

我想我明白你做了什么。

替换整个js代码
$(document).ready(function(){
    $.CookiesMessage({});
});
不要忘记

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

之后

答案 1 :(得分:0)

e.CookiesMessage这就是问题所在。由于e为undefined,因此您无法将其他对象绑定到其中。

试试这个:

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

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

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

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