Reputation: 921
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?
Upvotes: 3
Views: 1564
Reputation: 921
As @Pointy Suggested, the best Alternative to LocalStoage
for sharing data across frames is to use window.top
Property/Member/Object
But 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.top
Refer: Mozilla Dev Docs
Upvotes: 2
Reputation: 2064
Sharing data across multiple iframes that belongs to multiple domains can be done through postMessage
Upvotes: 1