<iframe id="id_description_iframe" class="rte-zone" height="200" frameborder="0" title="description">
<html>
<head></head>
<body class="frameBody">
test<br/>
</body>
</html>
</iframe>
What I want to get is:
test<br/>
<iframe id="id_description_iframe" class="rte-zone" height="200" frameborder="0" title="description">
<html>
<head></head>
<body class="frameBody">
test<br/>
</body>
</html>
</iframe>
What I want to get is:
test<br/>
The exact question is how to do it with pure JavaScript not with jQuery.
But I always use the solution that can be found in jQuery's source code. It's just one line of native JavaScript.
For me it's the best, easy readable and even afaik the shortest way to get the iframes content.
First get your iframe
And then use jQuery's solution
Select elements in iframe
Then you can usually use
getElementById()
or evenquerySelectorAll()
to select the DOM-Element from theiframeDocument
:Call functions in the iframe
Get just the
window
element fromiframe
to call some global functions, variables or whole libraries (e.g.jQuery
):Note
All this is possible if you observe the same-origin policy.
Using JQuery, try this:
AFAIK, an Iframe cannot be used that way. You need to point its src attribute to another page.
Here's how to get its body content using plane old javascript. This works with both IE and Firefox.
The following code is cross-browser compliant. It works in IE7, IE8, Fx 3, Safari, and Chrome, so no need to handle cross-browser issues. Did not test in IE6.
Chalkey is correct, you need to use the src attribute to specify the page to be contained in the iframe. Providing you do this, and the document in the iframe is in the same domain as the parent document, you can use this:
Obviously you can then do something useful with the contents instead of just putting them in an alert.
I think placing text inbetween the tags is reserved for browsers that cant handle iframes i.e...
You use the 'src' attribute to set the source of the iframes html...
Hope that helps :)