2

私は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 も含めますが、まだ Uncaught TypeError: Object function (e,t){return new x.fn.init(e,t,r)} has no method 'cookie というエラーが発生します'

4

2 に答える 2

0

デモjsFiddle

説明

すべてが意図したとおりに機能しているようです。


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;
};
于 2013-10-28T12:12:51.143 に答える