我有一个程序可以自动播放音乐并节省播放时间;当您浏览网站时,它会从保存点继续,而不是重新开始。
我想添加一个Cookie,以便当用户按下"音乐关闭"按钮,即使他们刷新页面也不会再次播放...反之亦然,所以如果他们再次打开它,即使刷新它也会保持打开状态。
我花了好几个小时,我仍然无法弄清楚......
这是我到目前为止所做的:
<style>
#chatter {
padding: 10px;
background-image: url();
background-color: #B8B8B8;
border: 1px solid #d4d0d9;
position: fixed;
bottom: 35px;
right: 35px;
z-index: 100;
display: none;
}
#nav {
line-height:1px;
background-color:#eeeeee;
height:25px;
width:100px;
float:left;
padding:3px;
}
#header {
position: relative;
min-height: 75px;
left: 1550px;
}
#header-content {
position: absolute;
bottom: 0;
left: 0;
}
#fixedcbox {
position:fixed;
bottom:0;
right:0;
}
#fixeddb {
position:fixed;
bottom:0;
left:0;
}
#clicker {
cursor: pointer;
}
</style>
<div id="fixedcbox">
<div id="clicker">
<input type ="button" id="player" value="Music: On" style="height:25px;width:100px" onclick="javascript:toggleSound();javascript:changeText;">
</button>
</div>
</div>
<audio id="audioToggle" preload="auto" src="http://se7en-ps.com/forums/public/Music/Christmas_song.mp3" loop="true" autobuffer>
</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>
<script type="text/javascript">
function changeText()
{
var value = document.getElementById("player").value;
if (value == "Music: On")
document.getElementById("player").value = "Music: Off";
else
document.getElementById("player").value = "Music: On";
}
function toggleSound() {
var audioElem = document.getElementById('audioToggle');
if (audioElem.paused)
audioElem.play();
else
audioElem.pause();
changeText();
}
</script>