2016-08-23 56 views
0

我正在處理EU cookie腳本,但似乎無法修復它。錯誤如下:Cookie腳本 - 未捕獲ReferenceError:e未定義

  • Uncaught ReferenceError: e is not defined

我在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拿過腳本。


編輯:修復上述錯誤後出現。

Uncaught TypeError: $.CookiesMessage is not a function

導致這個行:

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> 
+0

你在哪兒此行得到'e'變量替換整個你的js代碼? – Maxx

+0

你從哪裏得到這段代碼?您提供的網站有所不同。 – Maxx

+0

@Maxx這是腳本:http://www.jqueryscript.net/demo/Responsive-EU-Cookie-Law-Notice-Plugin-For-jQuery-Cookies-Message/dist/cookies-message.min.js - 我跑過JSBeautifier.org –

回答

0

e.CookiesMessage這就是問題所在。由於e是undefined,因此您無法將其他對象綁定到該對象中。

試試這個:

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

這很可能會解決您的問題。

現在,當我們都有點督察,eË處理出口的最有可能的一部分,爲此我們可以假設你複製/從別的地方粘貼此代碼。爲了使這個嘗試這個意義:

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

這很有效,謝謝。出現了一個不同的錯誤,TypeError:$。CookiesMessage不是一個函數,它位於head標籤內部,我將更新我的問題 –

1

我想我明白你做了什麼。

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

不要忘記

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

jQuery的裝載

+0

在jquery後面加載它,但我需要腳本在我的''中,所以我可以調整設置 –

+0

您可以將它放在head,只是在jQuery和插件之後。 – Maxx

+0

好的,在第60行,第60行是'Uncaught SyntaxError:Unexpected token',這一行是:'messageBg:「#151515」,' –

相關問題