Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda