Read IFrame content using JavaScript
Posted
by Rajat
on Stack Overflow
See other posts from Stack Overflow
or by Rajat
Published on 2010-05-05T22:52:51Z
Indexed on
2010/05/05
22:58 UTC
Read the original article
Hit count: 247
JavaScript
|iframe
Ok, This is my first time dealing seriously with IFrames and I cant seem to understand a few things:
First the sample code I am testing with:
<head>
<script type="text/javascript">
function init(){
console.log("IFrame content: " + window.frames['i1'].document.getElementsByTagName('body')[0].innerHTML);
}
</script>
</head>
<body onload="init();">
<iframe name="i1" src="foo.txt"/>
</body>
the file "foo.txt" looks like this:
sample text file
Questions:
1) The iframe seems to be behaving as a HTML document and the file text is actually part of the body instead. Why ? Is it a rule for an IFrame to be a HTML document. Is it not possible for the content of an iframe to be just plain text ??
2) The file content gets wrapped inside a pre tag for some reason. Why is this so ? Is it always the case?
3) My access method in the javascript is working but is there any other alternative? [native js solutions please] If the content is wrapped in a pre tag always then I will actually have to lookup inside the pre tag rather than lookup the innerHTML
© Stack Overflow or respective owner