Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get javascript variable in an iframe from parent window in a different domain

I have an iframe in a domain "someplace.com" being loaded into "anotherplace.com" 's window.

Old questions say you can't access variables across domains: Sharing global javascript variable of a page with an iframe within that page

But now that we have CORS, I'm wondering if this is possible. Are we now able to access JS variables in an iframe's parent if we enable CORS between the domains?

like image 246
Don P Avatar asked Apr 20 '26 17:04

Don P


1 Answers

You need to use the Web Messaging API.

https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage

like image 196
David Bradshaw Avatar answered Apr 22 '26 06:04

David Bradshaw



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!