【问题标题】:jQuery Cookie not working in ChromejQuery Cookie 在 Chrome 中不起作用
【发布时间】:2013-08-08 11:40:56
【问题描述】:

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

$(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

【问题讨论】:

    标签: jquery google-chrome cookies jquery-cookie


    【解决方案1】:

    在 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”来提供它。

    【讨论】:

    • 如果您可以在没有 https 的情况下生活,您可以使用 GitHub Pages 来提供您的内容。
    【解决方案2】:

    JQuery Cookie 在 Chrome 浏览器中不起作用。因此,今天经过数小时的分析,我找到了使用 javascript Storage API 来完成此任务的解决方案。我只是想在这里分享我的发现。您可以查看JQuery Cookie not working on chrome了解详情。

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 2018-11-03
      • 2012-03-09
      • 1970-01-01
      • 2015-04-05
      • 1970-01-01
      • 2011-02-28
      • 2013-03-18
      • 2011-11-19
      相关资源
      最近更新 更多