Error conditions
suggest changeIn browsers, blocking cookies often blocks localStorage as well, and reading it throws. This page is browser-only. querySelector returns null when no video exists, and reading volume from null also throws, so the catch is not only a storage failure. getItem returns a string or null.
const video = document.querySelector('video');
try {
const saved = localStorage.getItem('volume');
if (video && saved !== null) {
video.volume = Number(saved);
}
} catch (error) {
alert('Volume was not restored. Storage may be blocked.');
}
if (video) {
video.play();
}
See Error handling. An uncaught exception stops the rest of the script.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents