asimryu

local stroage

Jan 5th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.69 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="ko">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  6. <title>local storage example</title>
  7. </head>
  8. <body>
  9. <form>
  10. <input type="text" name="search" id="search">
  11. </form>
  12. <script>
  13. var ls;
  14. var storage;
  15. if(typeof(Storage) !== "undefined") {
  16.     ls = true;
  17.     storage = window.localStorage;
  18. } else {
  19.     ls = false;
  20.     storage = null;
  21. }
  22.  
  23. $(function(){
  24.     $("#search").keyup(function() {
  25.         if(ls){
  26.             storage.setItem("searchText", $(this).val());
  27.         }
  28.     });
  29. });
  30. window.onload = function(){
  31.     if(ls){
  32.         var searchText = storage.getItem("searchText");
  33.         $("#search").val(searchText);
  34.     }
  35. }
  36. </script>
  37.  
  38. </body>
  39. </html>
Advertisement
Add Comment
Please, Sign In to add comment