我有以下代码,尝试播放音频文件,然后在按下链接时,您可以打开或关闭它(它会保存一个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>