1

我有以下代码,它尝试播放音频文件,然后在按下链接时,您可以打开或关闭它(它会保存一个 cookie 以保存您的偏好)。出于某种原因,音频确实可以播放,但单击链接什么也没做。想法?我在想我错过的 cookie 部分有一些明显的东西,因为这是我第一次用 javascript 接触 cookie。

<p><a href="" onclick="Javascript:toggle()">Ääni päälle/pois</a><br />
<script>
window.onload=function(){
var pre='';
var ele=document.getElementById('test');
ele.play();
//when the song ends start a new one
ele.onended=function(){
  //if you are done with all the songs loop back to the beginning.
  //Or you could add some code to load more songs from the server
  ele.play();
}

}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function toggle() {
    if(!readCookie(sound)){
        createCookie(sound,false,100);
    }
    else if(readCookie(sound)==false){
        createCookie(sound,true,100);
        ele.play();
    }
    else if(readCookie(sound)==true){
        createCookie(sound,false,100);
        ele.pause();
    }
}

</script></p>
4

0 回答 0