Is it possible to call local storage inside a function and use it in Windows.onload? [closed]

Tags: , ,

I have a on click function which return ‘x’ and stores in div dynamically. After a page refresh, this dynamic div resets and the data is gone. But i want the data to stay. To do that i stored that in local storage and want to call later when page loads. I store it in local storage inside the function ‘test’ and calling it in windows.onload which is returning null. I understand that i am unable to call the local storage inside the function. My question: Is there a way to call the local storage inside the function ‘test’

function test(parameter1, parameter2) { // this is an onclick function
// some functionality
 return x;
 var test = x.innerHTML;
 localStorage.setItem('somediv', test);


window.onload = function () {

    var test2 = localStorage.getItem('somediv')



You are using localStorage fine.

The thing is, if you return x; in your text() function, the code below is never executed, so it never actually sets the localStorage variable.

That is why you get null when you are trying to access it.

Source: stackoverflow