I have the following HTML:
<audio autoplay id="background_audio">
<source src="https://sporedev.ro/pleiade/hol.mp3" type="audio/mpeg">
</audio>
<a href="#" id="mute">Play/mute sound</a>
And the following JS:
var audio = document.getElementById('background_audio');
document.getElementById('mute').addEventListener('click', function (e)
{
e = e || window.event;
audio.muted = !audio.muted;
e.preventDefault();
}, false);
Here you can find a JSFiddle.
What this script does is mute and resume playing an audio file that has the background_audio
id.
I spent the last day on cookies in JS on SO and Google but can't make things work (not knowing how to debug JS is the main problem I guess).
I need to create a cookie when clicking on the "mute
" id. Then I need to verify if the cookie is set or not and, if it's set, assign that value stored in the cookie to audio.muted. If the cookie is not set then it should play the sound.
While I know how to make conditionals and set cookies in PHP, JS is my weakspot and everytime I try something it ends up not working.
How can I modify the script in order to set a cookie or create a session so that when the user mutes the sound on one page it can stay that way on the others as well?
Could you please provide a working example of how it should be done?