document.cookie / setCookie没有在Safari和IE上工作

时间:2015-07-05 14:12:26

标签: javascript jquery cookies setcookie

我想在每个页面加载后从最后一个点播放音乐。这个例子不适用于Safari和IE,但在firefox,opera和chrome上运行正常。是否可以用jquery cookie插件替换它以及如何?

<audio preload="auto" src="a.mp3" loop="true" autobuffer autoplay="true">
    Unsupported in Firefox
    </audio>

    <script>

    function setCookie(c_name,value,exdays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
        document.cookie=c_name + "=" + c_value;
    }

    function getCookie(c_name)
    {
        var i,x,y,ARRcookies=document.cookie.split(";");
        for (i=0;i<ARRcookies.length;i++)
        {
          x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
          y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
          x=x.replace(/^\s+|\s+$/g,"");
          if (x==c_name)
            {
            return unescape(y);
            }
          }
    }

    var song = document.getElementsByTagName('audio')[0];
    var played = false;
    var tillPlayed = getCookie('timePlayed');
    function update()
    {
        if(!played){
            if(tillPlayed){
            song.currentTime = tillPlayed;
            song.play();
            played = true;
            }
            else {
                    song.play();
                    played = true;
            }
        }

        else {
        setCookie('timePlayed', song.currentTime);
        }
    }
    setInterval(update,1000);
    </script>

0 个答案:

没有答案