2013-08-07 92 views
0

我正在使用jQuery cookie插件。它在Firefox中工作正常。当我在Chrome中打开同一页面时,该值始终设置为未定义。我找不到问题所在。jQuery Cookie不能在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

回答

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

如果您的网页没有https,您可以使用[GitHub Pages](https://pages.github.com/)来提供您的内容。 –

0

JQuery Cookie在Chrome浏览器中不起作用。因此,今天经过几小时的分析,我发现了一个使用JavaScript Storage API来实现这一目标的解决方案。我只想在这里分享我的发现。你可以检查JQuery Cookie not working on chrome了解详情。

相关问题