Saturday, 5 December 2020

What is Local Storage Object? Explain with a best example.

Local Storage Object

In our previous section, we covered all the theoretical part of the storage. We had taken a quick view of all the methods of storage, which we will use for it. So, in this section, we will learn about how we can create and use the local storage object in PhoneGap. Below is the step by step procedure to create and use the local storage object.

1) Create a new project

Firstly, we will create a new PhoneGap project with a blank template. If you don't know how to create an app with a blank template, go through the PhoneGap project link.

Local Storage Object

After creating a PhoneGap project, we will open its index.html file contained in the www folder.

Local Storage Object

2) Adding JQuery mobile library

After creating the project, we will add the JQuery mobile library, as we have added in our previous examples. We will go to the JQuery, and from there, we will copy the three lines of code, i.e., CDN hosted files. We will paste this code in our index.html file and change the title in the following way:

  1. <!DOCTYPE html>  
  2. <html>  
  3.      <head>  
  4. <meta charset="utf-8">  
  5. <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">  
  6. <title>Local Storage Object Example</title>  
  7. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />  
  8.     <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>  
  9.     <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>  
  10.     </head>  
  11.     <body>  
  12.         <script type="text/javascript" src="cordova.js"></script>  
  13.     </body>  
  14. </html>  

3) Create a form

We will first create a form by creating a label and input field in the body section. After that, we will use this form to store the information. We will write all these things in the following way:

  1. <body>  
  2. <label for="crick">Favorite Cricketer</label>  
  3.         <input type="text" id="crick" />  
  4.             <script type="text/javascript" src="cordova.js"></script>  
  5. </body>    


Local Storage Object

4) Adding style/margin and divide the container

Our app is not looking so cool now. So, we will divide the container using <div></div> tag and set the margins for the container in the following way:

Note: We will do the code for the style and margins in the <style></style> tag in the head section.

  1. <head>  
  2. ;style>  
  3.     #container  
  4.     {  
  5.             margin: 10px;  
  6.     }  
  7.     </style>  
  8. </head>  
  9. <body>  
  10.     <div id="container">  
  11.     <label for="crick"> Favorite Cricketer </label>  
  12.     <input type="text" id="crick" />  
  13.     <script type="text/javascript" src="cordova.js"></script>  
  14. </body>     


Local Storage Object

5) Add button to save data.

Now, we will add a button in our form to save the data into the browser. So, when we will click on the button, the data will be stored into our browser. We will create a button below the input field using the <buuton></button> tag in the following way:

  1. <div id="container">  
  2.     <label for="crick"> Favorite Cricketer </label>  
  3.     <input type="text" id="crick" />  
  4.     <button id="btnSave"> Save Cricketer </button>  
  5.     <script type="text/javascript" src="cordova.js"></script>  
  6. </div>  


Local Storage Object

6) Getting data from the form

Now, we will grab the form data. For this purpose, we will use the <script></script> tags to create a custom script below the <style></style> tag. We will use the window.onload function, which is called when the screen gets loaded. In this function, we will get the button and use an event listener to callback the custom function storeData in the following way:

  1. <script>  
  2. window.onload = function()  
  3. {  
  4.         document.getElementById('btnSave').addEventListener('click', storeData);  
  5. }  
  6. </script>   

7) Creating storeData custom function

Now, we will create a custom function storeData by passing an argument. We will fetch the cricketer name from the form using the ID. After that, we will set the fetched data into the local storage in the following way:

  1. function storeData(e)  
  2. {  
  3. var cricketer = document.getElementById('crick').value;  
  4.     localStorage.setItem("favorite Cricketer", cricketer);  
  5. }  


Local Storage Object

We can check the local storage data using our browser.

Local Storage Object

We can also save other data under different key in the following way:

  1. var date = new Date();  
  2. localStorage.setItem("TimeStamp", date);  


Local Storage Object

8) Retrieving data

Now, we will create another button to retrieve the data in the same way as we have previously created for getting the data. After that, we will get the button using the ID that will be called using the function, i.e., getData. We will also add a field for the result using <div></div> tag in the following way:

Window.onload function:

  1. window.onload = function()  
  2. {  
  3.     document.getElementById('btnSave').addEventListener('click', storeData);        document.getElementById('btnRetrieve').addEventListener('click', getData)  
  4. }  

Body section:

  1. <body>  
  2.     <div id="container">  
  3.         <label for="crick"> Favorite Cricketer </label>  
  4.         <input type="text" id="crick" />  
  5.         <button id="btnSave"> Save Cricketer </button>  
  6.         <script type="text/javascript" src="cordova.js"></script>  
  7.         <div id="result"></div>  
  8.         <button id="btnRetrieve"> Retrieve Cricketer </button>  
  9.     </div>  
  10. </body>  


Local Storage Object

9) Creating getData function

We will create the getData function in the <script></script> tag that will be called by the retrieve button. We will use the localStorage.getItem() function to get data from the local storage and show the result using the ID. The complete custom script is shown below.

  1. <script>  
  2.     window.onload = function()  
  3.     {  
  4.         document.getElementById('btnSave').addEventListener('click', storeData);  
  5.         document.getElementById('btnRetrieve').addEventListener('click', getData)  
  6.     }  
  7.         function storeData(e)  
  8.     {  
  9.         var cricketer = document.getElementById('crick').value;  
  10.         localStorage.setItem("favorite Cricketer", cricketer);  
  11.         var date = new Date();  
  12.         localStorage.setItem("TimeStamp", date);  
  13.     }  
  14.     function getData(e)  
  15.     {  
  16.         var crick = localStorage.getItem('favorite Cricketer');  
  17.         document.getElementById('result').innerHTML = crick;  
  18.     }  
  19. </script>  


Local Storage Object














No comments:

Post a Comment