Guest User

Untitled

a guest
Dec 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. function initializeStorage() {
  2. if (typeof localStorage.recipes == "undefined") {
  3. localStorage.recipes = "[]";
  4. }
  5. if (localStorage.recipes == "[]") {
  6. $("#localStorage").css("display", "none");
  7. }
  8. if (localStorage.recipes != "[]") {
  9. printStorage();
  10. }
  11. }
  12.  
  13. function addStorage() {
  14. var x = document.getElementById("product_name").value.trim();
  15. if (x == "") {
  16. alert("Product name must be filled out");
  17. return false;
  18. }
  19. var currentdate = new Date();
  20. var datetime =
  21. currentdate.getDate() +
  22. "/" +
  23. (currentdate.getMonth() + 1) +
  24. "/" +
  25. currentdate.getFullYear() +
  26. " @ " +
  27. currentdate.getHours() +
  28. ":" +
  29. currentdate.getMinutes() +
  30. ":" +
  31. currentdate.getSeconds();
  32.  
  33. var u = JSON.parse(localStorage.recipes);
  34. var o = {
  35. product: x,
  36. date: datetime
  37. };
  38.  
  39. u.unshift(o);
  40. var len = u.length;
  41. while (len > 5) {
  42. u.pop();
  43. len--;
  44. }
  45. localStorage.recipes = JSON.stringify(u);
  46. return true;
  47. }
  48.  
  49. function printStorage() {
  50. var u = JSON.parse(localStorage.recipes);
  51. var l = u.length;
  52. var s = String("<h3>Search History: </h3>");
  53. var i = 0;
  54. while (i < l) {
  55. s +=
  56. "<div><strong>Product: </strong>" +
  57. "<span style='display:inline'>" +
  58. u[i].product +
  59. "</span>" +
  60. "<strong> Searched at: </strong>" +
  61. "<span style='display:inline'>" +
  62. u[i].date +
  63. "</div>";
  64. i++;
  65. }
  66. document.getElementById("localStorage").innerHTML = s;
  67. return true;
  68. }
Add Comment
Please, Sign In to add comment