我正在写一个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但仍然出错 未捕获的TypeError:对象函数(e,t){return new x.fn.init(e,t,r)}没有方法'cookie'
答案 0 :(得分:7)
给出错误Uncaught TypeError:对象函数(e,t){return new x.fn.init(e,t,r)}没有方法&#39; cookie&#39;
好的,这告诉我们你做加载了jQuery(因为它是缩小的jQuery函数的样子)并且jQuery正在使用$
符号,但是某些原因,当您运行该代码时,jQuery函数上不存在cookie插件。可能的原因:
您使用Cookie插件的路径不正确,而且您获得了404。
在加载Cookie插件后,您首次加载jQuery (第一次jquery.min.js
在jquery.cookie.js
之后)或您#39 ;不小心加载了秒时间,覆盖了第一个。
您在加载jQuery之后运行代码,但在加载Cookie插件之前
您已按照正确的顺序排列script
个标签,并且已使用the async
attribute,因此他们无法执行此操作。
您正在使用代码(非标记)添加script
元素。使用代码而不是标记添加脚本时,无法保证其执行顺序。
如果你有这个:
<script src="/path/jquery.min.js"></script>
<script src="/path/jquery.cookie.js"></script>
<script src="/path/your.script.js"></script>
...而且你没有得到任何404错误,它应该有效。
答案 1 :(得分:0)
<强>描述强>
一切似乎都按预期工作。
<强> JS 强>
$(function(){
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.msdproduct.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;
};