What exactly does "contentDocument" represent for an iframe (or even the old "frame" element)? Is it equivalent to the "html" element or the "body" element ? What is it's use? And is this property supported across all the browsers?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
w3.org
MDN
msdn
So to get the innerHTML of the body element you could use
iframe.contentDocument.getElementsByTagName("body")[0]
or
iframe.contentDocument.body
in todays browsers.
contentDocument
represents the document of an iframe (DOM object). It is not equivalent tohtml
since documents have their own properties, however if you type:You will get the body itself.
It is supported in all the browsers, with a tiny modification: for Internet Explorer use
Enjoy, Nili
contentDocument
is the standardized way to get hold of the iframe or frame'sDocument
object. It is the same object as JavaScript running within the iframe would access viadocument
.As noted in other answers, IE didn't support it until version 8 but did support access to the iframe's
Window
object viacontentWindow
. A cross-browser way of getting hold of an iframe's<body>
element is therefore:Note that if the iframe is not served from the same domain as the main document, browser security restrictions will prevent access to its document object in this or any other way.