Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 data-attributes in iframe element

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.

like image 393
izk Avatar asked Nov 26 '12 18:11

izk


People also ask

Which iframe attributes are not supported in HTML5?

Note − The frameborder, marginwidth, longdesc, scrolling, marginheight attributes deprecated in HTML5.

Are the attributes of iframe tag?

Attributes used with iframe are srcdoc, name, src.

Does HTML5 support iframe?

The <iframe> element is still valid in HTML5. Depending on what exact interaction you need there might be different APIs. For example there's the postMessage method which allows you to achieve cross domain javascript interaction.


1 Answers

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.

like image 111
user123444555621 Avatar answered Sep 30 '22 15:09

user123444555621