jQuery Cookie无法在Chrome中运行

时间:2013-08-07 07:48:00

标签: jquery google-chrome cookies jquery-cookie

我正在使用jQuery cookie插件。它在Firefox中运行良好。当我在Chrome中打开同一页面时,该值始终设置为undefined。我找不到问题所在。

$(document).ready(function () {
    alert($.cookie('Brit_vidests'));
    if ($.cookie('Brit_vidests') != '1') {

        var id = '#dialog';

        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        $("body").append('<div id="mask"/>');
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.7);
        var winH = $(window).height();
        var winW = $(window).width();
        $(id).css('top', winH / 2 - $(id).height() / 2 - 20);
        $(id).css('left', winW / 2 - $(id).width() / 2 - 20);
        $(id).fadeIn(2000);
        $('.window .close').click(function (e) {
            e.preventDefault();
            $('#mask').hide();
            $('.window').hide();
            $('#mask').remove();
        });

        //if mask is clicked
        $('#mask').click(function () {
            // $(this).hide();
            //$('.window').hide();
            //$(this).remove();
        });


        $.cookie('Brit_vidests', '1', { expires: 60 });
    }

});

JSFiddle

2 个答案:

答案 0 :(得分:2)

在Chrome中打开控制台,您将看到问题所在:

Resource interpreted as Script but transferred with MIME type text/plain:
"https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js".
Refused to execute script from 'https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.

您直接从github嵌入脚本,但github使用MIME类型“text / plain”而不是“text / javascript”来提供它。

答案 1 :(得分:0)

JQuery Cookie在Chrome浏览器中不起作用。因此,经过数小时的分析,我找到了一个使用javascript Storage API来实现这一目标的解决方案。我只想在这里分享我的发现。您可以查看JQuery Cookie not working on chrome了解详细信息。