Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Audio player. How to add cookies to JS code?

Made from different pieces of code an audio player in JS. He works great. There are two button positions to turn on the sound and turn off the sound. So when you open another page of the site, the position of the button is always on. How to add a cookie method, if the button should be turned off so that it is remembered and the music does not sound? Help me please.

var radio = new Audio();
radio.src = "https://site.dvasyl.com/wp-content/uploads/2022/05/preview.mp3";
radio.loop = true;
radio.play();

document.querySelector('#muted').onclick = function() {
  if (radio.muted === true) {
   document.querySelector('#muted').innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" />'
    radio.muted = false;
  } else {
    document.querySelector('#muted').innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_off-1.svg" alt="On" />'
    radio.muted = true;
  }  
}
    
    .size_sound_alert{
        width: 30px;
    }
    
.sound_label{
    position: fixed;
    margin-bottom: %;
    margin-left: 95%;
    top: 90%;
    z-index: 9999;
}
    
@media screen and (max-width: 768px) {
    .sound_label {
        position: fixed;
        margin-bottom: %;
        margin-left: 85%;
        top: 90%;
    }
}
<div id="muted" class="sound_label"><img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" /></div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can set the cookie to whatever you need in your onClick event.

document.cookie adds a cookie to the browser.

<script>

var radio = new Audio();
radio.src = "https://site.dvasyl.com/wp-content/uploads/2022/05/preview.mp3";
radio.loop = true;
let isMuted = document.cookie;
radio.muted = isMuted == "muteCookie" ? true : false;
radio.play();

document.querySelector('#muted').onclick = function() {
  var soundAlert = document.querySelector('#muted');
  if (radio.muted) {
    soundAlert.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" />'
    radio.muted = false;
    document.cookie = "";
  } else {
    soundAlert.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_off-1.svg" alt="On" />'
    radio.muted = true;
    document.cookie = "muteCookie";
  }  
}

</script>
about 4 years ago · Juan Pablo Isaza Report

0

// add elem to variable for single select from the dom;
const mutedElem = document.getElementById('muted');

var radio = new Audio();
radio.src = "https://site.dvasyl.com/wp-content/uploads/2022/05/preview.mp3";
radio.loop = true;

// you can't play audio without user action
// https://developer.chrome.com/blog/autoplay/
// radio.play();

// get value from localStorage and mute player if we need
if (!!localStorage.getItem('muted')) playerMute();

mutedElem.addEventListener('click', function () {
    // toggle muted param
    if (radio.muted) playerUnmute();
    else playerMute();
})

// mute player function
function playerMute() {
    mutedElem.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_off-1.svg" alt="On" />'
    radio.muted = true;
    localStorage.setItem('muted', 'true');
}

// unmute player function
function playerUnmute() {
    mutedElem.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" />'
    radio.muted = false;
    localStorage.removeItem('muted');
}
body {
  background: gray;
}
<div id="muted" class="sound_label">
  <img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off"/>
</div>

You had better use localStorage like in my answer.

p.s. you can't play audio without user action (please read this doc)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!