Advertisement
Guest User

Untitled

a guest
Sep 13th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. <!DCOTYPE html>
  2. <head>
  3. <title>Test</title>
  4. <script src="js/jquery-1.11.3.min.js"></script>
  5.  
  6. <script>
  7.  
  8. $(document).ready(function () {
  9. /*
  10. // dynamic-length record, delimiter
  11. localStorage.contact = "Chayapol|Moemeng|pic.jpg|08989999|mchayapo@gmail.com";
  12.  
  13. // tokenize contact with |, NAH
  14. */
  15.  
  16. // JSON - JavaScript Object Notation
  17. var contactData = {
  18. "contacts": [
  19. {
  20. "firstname": "Chayapol",
  21. "lastname": "Moemeng",
  22. email: "mchayapol@scitech.au.edu",
  23. phone: "089999999",
  24. avatar: "avatar.png"
  25. },
  26. {
  27. "firstname": "Anna",
  28. "lastname": "Smith",
  29. email: "anna@scitech.au.edu",
  30. phone: "088888888",
  31. avatar: "avatar.png"
  32. }
  33. ]
  34. };
  35.  
  36. // Simple example
  37. var contactA = {
  38. "firstname": "Anna",
  39. "lastname": "Smith",
  40. email: "anna@scitech.au.edu",
  41. phone: "088888888",
  42. avatar: "avatar.png"
  43. };
  44.  
  45. console.log(contactA);
  46.  
  47. // Load all contacts into div2
  48. console.log(localStorage.contactList);
  49. // It could happen that localStorage.contactList has never been defined.
  50. // So, you'll get 'undefined'.
  51.  
  52. var list;
  53. if (typeof localStorage.contactList === "undefined") {
  54. list = [];
  55. } else {
  56. list = JSON.parse(localStorage.contactList);
  57. }
  58.  
  59. var listHTML;
  60. for (var c in list) {
  61. console.log('c'+c);
  62. listHTML += "<li>" + c.firstname + " " + c.lastname + "</li>";
  63. }
  64. $('#div2').html("<ul>" + listHTML + "</ul>");
  65.  
  66. //localStorage.contact = JSON.stringify(contactA);
  67. var tempContact = JSON.parse( localStorage.contact );
  68.  
  69. console.log(tempContact);
  70.  
  71. $('#firstname').val(tempContact.firstname);
  72. $('#lastname').val(tempContact.lastname);
  73. $('#name').html( );
  74.  
  75. // Add button event handler (an annonymous callback function)
  76. $('#btn1').click(
  77. function () {
  78. // Add to the list and
  79. // Update the current localStorage.contactList
  80. tempContact.firstname = $('#firstname').val();
  81. tempContact.lastname = $('#lastname').val();
  82. console.log($('#firstname').val() + '--' + tempContact.firstname);
  83.  
  84. list[list.length] = tempContact;
  85. localStorage.contactList = JSON.stringify(list);
  86.  
  87. console.log(JSON.stringify(tempContact));
  88. localStorage.setItem("contact",JSON.stringify(tempContact));
  89. }
  90. );
  91. });
  92.  
  93. </script>
  94.  
  95. </head>
  96. <html>
  97. <body>
  98. <div id="div1">
  99. <input type="text" id="firstname" /><br />
  100. <input type="text" id="lastname" /><br />
  101. <span id="name"></span>
  102. <button id="btn1">Add Contact</button>
  103. </div>
  104. <div id="div2"></div>
  105.  
  106. </body>
  107.  
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement