Some JS API examples for people who are learning javascript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.2 KiB

2 years ago
  1. var dataToStore = [
  2. "file___0001.mp4",
  3. "file___0002.mp4",
  4. "file___0003.mp4",
  5. "file___0004.mp4",
  6. "file___0005.mp4",
  7. "file___0006.mp4",
  8. "file___0007.mp4",
  9. "file___0008.mp4",
  10. "file___0009.mp4",
  11. ]
  12. let configKey = "video-files";
  13. var restoreHere = null; // variable where we will restore the data that we read from localStorage
  14. // clear all items for this origin on localStorage
  15. for (var item in localStorage) delete localStorage[item]
  16. // store the contents of the variable `dataToStore` in the localStorage
  17. // data to be stored needs to be a string, so we JSON.stringify our data
  18. localStorage.setItem(configKey, JSON.stringify(dataToStore))
  19. // check if the browser supports localStorage
  20. if (typeof(Storage) == "undefined" || !localStorage) {
  21. alert("localStorage not supported in this browser")
  22. }
  23. // this is how we retrieve our data from the browser's localStorage
  24. let storedString = localStorage.getItem( configKey )
  25. let body = document.getElementById('result')
  26. if(storedString) {
  27. restoreHere = JSON.parse( storedString )
  28. // print results of fetch on webpage
  29. body.innerHTML = `<quote>${restoreHere}</quote>`
  30. } else {
  31. body.innerHTML = '<em>'+ `couldn't find anything in localStorage with key '${configKey}'` +'</em>'
  32. }