@amalkarim
Posted
Hi again, Carol
It's quite simple to store data in a local storage. In your sandbox.js
file, add this code just before the catch
block:
localStorage.setItem('lastAdviceId', userid);
localStorage.setItem('lastAdviceText', useradvice);
It will store the current useradvice
and userid
variables which we could get later.
Then, when the page is loaded, we need to retrieve those two variables (if available) and write it on the page. Add this code below:
window.onload = function() {
const advice= document.querySelector('.advise');
const generateAdvice= document.querySelector('.advisegenerated');
if (localStorage.getItem('lastAdviceId')) {
generateAdvice.innerText= `"${localStorage.getItem('lastAdviceText')}"`
advice.querySelector('span').innerText= `#${localStorage.getItem('lastAdviceId')}`;
}
}
Please remember though, that localStorage
will still store the data even after the browser is closed. You can read about it and its alternative storage in this w3schools article.
Alternatively, we could run the function that get the advice from adviceslip.com and render them in the page at window.onload
, thus you don't need to write "default #1 advice" from the start.
Hope this helps. Happy fun coding!
Marked as helpful
@Carolkiarie
Posted
@amalkarim This has definitely helped. Thank you so much.