I'm creating a scoreboard in html that is controlled by another html page. I work with messages like proposed in the answer of my previous post (display text on a child window in javascript) It is working really good and I can update text. Now I'm trying to change the source of an image.
main.js
const setFaute1 = (faute1) => {
afffaute1.innerHTML = faute1;
switch (faute1) {
case 1:
faute11.src = './images/FillFault.png';
break;
case 2:
faute12.src = './images/FillFault.png';
break;
case 3:
faute13.src = './images/FillFault.png';
break;
case 0:
faute11.src = './images/EmptyFault.png';
faute12.src = './images/EmptyFault.png';
faute13.src = './images/EmptyFault.png';
break;
}
bc.onmessage = (messageEvent) => {
switch (messageEvent.data) {
case 'update_mot':
setMot(localStorage.getItem('mot'));
break;
case 'update_equipe1':
setEquipe1(localStorage.getItem('equipe1'));
break;
case 'update_equipe2':
setEquipe2(localStorage.getItem('equipe2'));
break;
case 'update_score1':
setScore1(localStorage.getItem('score1'));
break;
case 'update_score2':
setScore2(localStorage.getItem('score2'));
break;
case 'update_faute1':
setFaute1(localStorage.getItem('faute1'));
break;
case 'update_faute2':
setFaute2(localStorage.getItem('faute2'));
break;
default:
}
};
btnfaute1.onclick = (e) => {
var value = parseInt(afffaute1.innerHTML, 10);
if (value < 3) {
value++;
} else {
value = 0;
}
localStorage.setItem('faute1', value);
afffaute1.innerHTML = value;
setFaute1(value);
bc.postMessage('update_faute1');
};
};
It displays correctly on the page where the button is but not on the second page. The text field updates correctly.
I don't understand why. Can someone help me?
Full code : https://gitlab.com/roger.julien/tableau-affichage-impro/-/tree/fautes
I found the solution myself. The localstorage stores string. And my case was comparing integer. I just had to convert the string to an integer and it works.