I am working thru the tutorial titled JavaScript Basics, found here:
I have worked through most of it, and here is my current code:
`
/const myHeading = document.querySelector(‘h1’);
myHeading.textContent = ‘Hello world!’;/
let myImage = document.querySelector(‘img’);
myImage.onclick = function() {
let mySrc = myImage.getAttribute(‘src’);
if(mySrc === ‘images/firefox-icon.png’) {
myImage.setAttribute(‘src’, ‘images/Icon_Atomic_256x256.png’);
} else {
myImage.setAttribute(‘src’, ‘images/firefox-icon.png’)
}
}
let myButton = document.querySelector(‘button’);
let myHeading = document.querySelector(‘h1’);
function setUserName() {
let myName = prompt(‘Please enter you name.’);
localStorage.setItem(‘name’, myName);
myHeading.textContent = 'Mozilla is cool, ’ + myName;
}
if(!localStorage.setItem(‘name’)) {
setUserName();
} else {
let storedName = localStorage.getItem(‘name’);
myHeading.textContent = 'Mozilla is cool, ’ + storedName;
}
myButton.onclick = function() {
setUserName();
}
`
It doesn’t work. It does nothing. The error I get in the browser’s console is “Uncaught DOMException: Failed to read the ‘localStorage’ property from ‘Window’: Access is denied for this document.
at file:///home/beginner-html-site-styled-gh-pages/scripts/main.js:24:1”
However, in Atom, the error is a bit more specific: “Uncaught TypeError: Failed to execute ‘setItem’ on ‘Storage’: 2 arguments required, but only 1 present.
at main.js:24”
So it seems to expect 2 arguments for if(!localStorage.setItem('name'))
Your version works in my browser just fine. Why doesn’t mine?