This may sound ridiculous but there are some situations when we need to share JavaScript Objects across iframe.
When we try to use localStorage for serving the purpose, people say that some browsers might not support it.Even if they support localStorage, Users might’ve reduced the storage capacity and so forth…
So my question is, Do we have a Solution/Alternative to share JavaScript Objects across iframe
Below example might get us on the same page
parentpage.html
<html>
...
<head><script src="script1.js" type="text/Javascript"></script></head>
<body>
...
<iframe src="childpage.html"></iframe>
</body>
</html>
childpage.html
<html>
...
<head><script src="script2.js" type="text/Javascript"></script></head>
...
</html>
note:
parentpage.html , childpage.html, script1.js, script2.js reside in the same domain and in the same location
Assume that script1.js uses a JavaScript Object that needs to be made publicly available.
Since the browser/UserAgent creates a separate Window Object for each additional iframe used in the page and we can’t access the Public JavaScript Object if we attach/Extend it to the Window Object/Document Object, The Last hope of it seems to fade away!
Is there any Solution/Workaround?
As @Pointy Suggested, the best Alternative to
LocalStoagefor sharing data across frames is to usewindow.topProperty/Member/ObjectBut still, the Property is ReadOnly as mentioned in mozilla dev docs but I still wonder if it’s not in anyway forced strictly in the future not to add new Member/Field to
window.topRefer: Mozilla Dev Docs