Take the 2-minute tour ×

Can custom data-attributes set on an iframe be accessed by the iframe's content?


<iframe src="foo.html" data-something="123"></iframe>

Is there a way to access data-something's value form the iframe's document?


share|improve this question

2 Answers 2

up vote 6 down vote accepted

Yes, use the frameElement attribute.


Demo: http://jsfiddle.net/Ehj2Q/

Of course this does not work at all if the iframe's content are from a different origin.

share|improve this answer
This is great, but yeah, not the same domain. I guess the iframe's document can't read it's parent tag's attributes at all then. –  izk Nov 26 '12 at 23:46
That's right. The only thing that both can access is the 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. –  Pumbaa80 Nov 27 '12 at 0:02

you should be able to access it using

var ifr_data = document.getElementById('myframe').data-something;

provided that data-something is a valid iframe attribute

share|improve this answer
This would be from the iframe's container window, not from the iframe's document as asked. –  izk Nov 26 '12 at 23:45

Your Answer


