Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript storing/state of global data/object

Is there a way to store global data in the window object such that the data can survive page reloads/refresh. So lets say I assign my global data/object -

window.myObject = myProductObject

And the user refreshes/reloads the page or may be jumps to another page of my website. Is window.myObject still available after the page reload?

NOTE -: I cannot store the object in a cookie, since its a object, I mean it could be a reference to another custom object or it could refer to another "window" object which has opened via "window.open"

like image 246
kapso Avatar asked Oct 20 '25 16:10

kapso


1 Answers

Use the window.top.name hack

var data = window.top.name ? JSON.parse(window.top.name) : {}
...
window.top.name = JSON.stringify(data)

window.top.name persists across page loads

I recommend you use an abstraction like lawnchair instead though

like image 140
Raynos Avatar answered Oct 23 '25 06:10

Raynos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!