[Solved] How to Pass Data to Another Page using JavaScript LocalStorage?

[Solved] How to Pass Data to Another Page using JavaScript LocalStorage?

In this quick guide, I will share the code to pass data to another page using JavaScript localStorage. Before that, you should know why do you need localStorage and how does it work.

Why localStorage?

Usually, we can pass the data from one page to another page as a parameter in the URL. Passing those values in URL has certain disadvantages.

  • If the data is too big, a URL with a long string doesn’t look good.
  • Displaying important data in the URL to the user can be harmful to your data security.

Another way is to use the POST method. For this, you need to create an HTML form.

If you want to avoid passing data into the URL or creating a form, leverage the use of localStorage with the simple JavaScript.

So using localStorage,

  • You don’t need to pass the data in URL.
  • You don’t need to create the HTML form.

What is localStorage in JavaScript?

It is a property that allows you to store the data in a web browser for a session.

How does localStorage work?

With the localStorage property, we can save the data into the browser. You can retrieve that data for the same or for any other web page.

Here, local storage acts as an intermediate to save and read the data.

JavaScript has simple functions to save and get the data. All the data saved in local storage is in the form of a key-value pair.

How to Pass the Data to Another Page withing passing it in URL?

HTML and Javascript Code for testing localStorage:

<!DOCTYPE html>
<html>
<body>

<script>
    // Check browser support
    if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("programming", "Python");
    }
</script>

</body>
</html>

Save it as first.html.

It will save the data (programming(key)/Python(value)) in the local browser.

Note: It is always good practice to check if your browser supports the localStorage property, though it is supported by most of the popular browsers.

Now let’s write an HTML and JavaScript code to read this data.

<!DOCTYPE html>
<html>
<body>

<div id="result"></div>

<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("programming");
} else {
document.getElementById("result").innerHTML = "Browser does not support Web Storage.";
}
</script>

</body>
</html>

Save it as second.html.

Save both the files in a common directory. And open them in browser tabs. Using second.html, You are able to read the value you have set for programming using first.html.

Note: In the above code, we have added JavaScript code inside the HTML page itself. You can add it externally as well.

Important Points to Note about localStorage:

  • All the data saved using localStorage is saved in the key-0value pair.
  • It is a read-only property.
  • Almost all the latest versions of popular browsers support this property.
  • The data saved using this property does not have an expiration date.
  • It is different from the sessionStorage JavaScript property. It saves the data for a particular session.
  • Unlike sessionStorage, data saved using localStorage will not be deleted on the closing browser tag.

Other important topics from HTML:

This is all about a quick tutorial to pass data to another page using JavaScript localStorage. If you have any question, feel free to discuss in the comment.

4 Comments

  1. Sir, I want to send data from one webpage to another webpage.
    Will you please send me the code how can I do it?
    I tried a lot but I couldn’t.
    Sir, please send me the coding in my email.
    the email is (email id is hidden.)

    1. Hi Mukesh,

      There can be multiple ways based on how you make the interface between those two web pages.

      If you add a link from one page to another, you can pass the data in the request body or the URL parameters. If you don’t have an interface and want those two web pages to work independently, save the data from one page (maybe inside the database or some text file.) Use that data in your second webpage.

Leave a Reply

Your email address will not be published. Required fields are marked *