JavaScript Local Storage not showing the correct value in another file

Related searches

I have three files

  1. main.js
  2. index1.html
  3. index2.html

I want to get the length of the number of divs from index2.html and store it in main.js, so I can use it with index1.html. When I console.log it shows the correct value which is 24 in index2.html but the value in index1.html is 0.

I linked main.js to both HTML files

var popC = $('body > div.pro-pop').length;
localStorage.setItem('popC', popC);
var unco = parseFloat(localStorage.getItem('popC'));
console.log('unco:', unco);

I think you are using jQuery in JavaScript that won't work but in pure JavaScript you can you local storage like this way. Thank You! Hope it helps you.

<!DOCTYPE html>
<html>
<body>

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

<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
  // Store
  localStorage.setItem("lastname", "Smith");
  // Retrieve
  document.getElementById("result").innerHTML = localStorage.getItem("lastname");
} else {
  document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>

</body>

LocalStorage, sessionStorage, Web storage objects localStorage and sessionStorage allow to save Unlike cookies, web storage objects are not sent to server with each set the data in one window, the change becomes visible in another one. newValue – the new value ( null if the key is removed). url – the url of the document where� The process for saving and retrieving local storage has two parts. First you must be able to retrieve the contents of local storage in a form that is manageable in javascript. Since local storage is a map of key-value pairs the easiest way to this is to turn local storage into a javascript object. Then take this object and turn it into a JSON

the localStorage save the values per domain. not per page. if you want the values to be different in different pages, you need to give it different keys. you can try prepend the pathname before the key, so in each page it will be different.

 localStorage.setItem(document.location.pathname +'popC', 9);
 var unco = parseFloat(localStorage.getItem(document.location.pathname + 'popC'));
 console.log(unco);

Window.localStorage, The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions. time, data stored in sessionStorage gets cleared when the page session ends or the origin is not a valid scheme/host/port tuple (this can happen if the� To use this value, you would have to convert it back to an object. To do this, we make use of the JSON.parse() method, which converts a JSON string into a JavaScript object. JSON.parse(window.localStorage.getItem('user')); removeItem() When passed a key name, the removeItem() method will remove that key from the storage if it exists. If there

if your main.js contains this below js code - I suggest you to not to include this in index1.html because it updates your value again. Localstorage data is based on the domain you can create it on any page and retrieve it on any page with the same domain.

       var popC = $('body > div.pro-pop').length;
       localStorage.setItem('popC', popC);
       var unco = parseFloat(localStorage.getItem('popC'));
       console.log('unco:', unco);

How to Use Local Storage with JavaScript, Basic knowledge of working with the DOM (optional). Create a "new tab page" application that stores to-do items locally in the Local storage - data with no expiration date that will persist after the browser window is closed. I'll use a generic key and value for the names, but they can be any strings. Another neat thing about local storage is that it doesn't have as many size constraints as cookies. Local storage provides at least 5MB of data storage across all major web browsers, which is a heck of a lot more than the 4KB (maximum size) that you can store in a cookie.

Get And Set Variable Values From One JavaScript File To Another , All pages can store and access the data stored in Local Storages. The implementation and use of Local Storage is shown below. My A.js and B.js� In this article, we will explore how to get and set Variable Values from One JavaScript File to another JavaScript File Using Local Storage, I will demonstrate the use by using two JavaScript files. Let’s say we have two files - A.js and B.js. I had this challenge where I had to find some way to obtain a value of a variable

Window localStorage Property, The localStorage and sessionStorage properties allow to save key/value pairs in a web browser. The localStorage object stores data with no expiration date. Please note that the event also contains: event.url – the url of the document where the data was updated. Also, event.storageArea contains the storage object – the event is the same for both sessionStorage and localStorage, so event.storageArea references the one that was modified.

For example, for one of my partitions, Storage Usage reports that I have 15.5 Gb of Pictures on the drive. Clicking on that selection to show more details confirms the 15.5 Gb value but shows less than one Gb in the list of folders (see screenshot). I get similar results for the other file types such as Documents and Music.

Comments
  • You are trying to set the value before getting the value