Can custom data-attributes set on an iframe be accessed by the iframe's content?
Example:
<iframe src="foo.html" data-something="123"></iframe>
Is there a way to access data-something's value form the iframe's document?
Thanks.
Can custom data-attributes set on an iframe be accessed by the iframe's content?
Example:
<iframe src="foo.html" data-something="123"></iframe>
Is there a way to access data-something's value form the iframe's document?
Thanks.
Yes, use the frameElement
attribute.
window.frameElement.getAttribute('data-something');
Demo: http://jsfiddle.net/Ehj2Q/
Of course this does not work at all if the iframe's content are from a different origin.
src
, which is reflected in the frame content's location
. So the parent frame can send data using the hash foo.html#something=123
, like Closure does. For modern browsers, there's postMessage. But again, this is only possible if the parent frame opts in. –
Grayling © 2022 - 2024 — McMap. All rights reserved.