Easing Back into Coding for the Web - Wednesday, Nov 30, 2022
- Click here for the W3School tutorial on HTML 5 Web Storage
- Create a new html document, using our starter template.
- Create a javascript function named getInfo() in the script area.
- In the <body> tag, add an onload event handler to trigger the getInfo function when the page loads.
- In the javascript function, add this code:
var firstName = prompt("What is your first name?");
- Add another line similar to the one above to ask the user for their last name.
- Again, in the javascript, add this line to store the user's first name:
localStorage.setItem("firstName", firstName);
- Do the same for lastName.
- Create another html document, using the template.
- Add two <p> elements, one with id="fName", the other with id="lName".
- Create a javascript function called loadInfo() and have it get triggered by an onload event.
- In the loadInfo() function add this line of code:
document.getElementById("fName").innerHTML = "Welcome back, " + localStorage.firstName;
- Repeat the above step for lastName.
- Add code to each page to link them.
- Can you think of anything that could go wrong?