...

View Full Version : Read/Write to/from iframe



rsp12uk
12-18-2003, 11:44 AM
Hi everyone,

I am having problems with accessing the innet HTML code of iframe tag. What I'm trying to do is:

var htmlcode = iframe.document.body.innerHTML;

The debugger says that the object doesn't support this property or function.

Thanks a lot everyone

liorean
12-18-2003, 12:08 PM
First of all, you must make sure that the variable iframe you use is actually a DOM reference to the iframe. Second, you access the wrong property, you should use HTMLIFrameElement's contentDocument in DOM compliant browsers.

rsp12uk
12-18-2003, 12:26 PM
I am kind of not the best JS programmer. I use IE 5.5+. What's DOM?

Thanks

adios
12-18-2003, 06:10 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>untitled</title>
<style type="text/css">

iframe {
display: block;
margin: 40px auto;
width: 440px;
height: 260px;
border-width: 0;
}

div {
text-align: center;
}

</style>
<script type="text/javascript" language="javascript">

function get_IFRAME_doc(iframe_id)
{
var oIframe = document.getElementById(iframe_id);
if (oIframe != null)
{
if (oIframe.contentDocument)
return oIframe.contentDocument;
else if (oIframe.contentWindow)
return oIframe.contentWindow.document;
else if (oIframe.document)
return oIframe.document;
else return null;
}
}

function write_to_iframe(iframe_id)
{
sHTML = '<body style="font:14px verdana;color:white;background:purple;">';
sHTML += '<h4>Iframe Content</h4>';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += 'bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ';
sHTML += '<h5>The end.</h5></body>';
var oIframe = document.getElementById(iframe_id);
oIframe.src = 'javascript&#58;parent.sHTML';
}

function read_from_iframe(iframe_id)
{
var oIframe = document.getElementById(iframe_id);
if (IFrameDoc = get_IFRAME_doc(iframe_id))
alert(IFrameDoc.getElementsByTagName('BODY').item(0).innerHTML);
}

</script>
</head>
<body>
<iframe name="mishmash" id="mishmash" frameborder="0"></iframe>
<div>
<button onclick="write_to_iframe('mishmash')">write to <strong>iframe</strong></button>&nbsp;
<button onclick="read_from_iframe('mishmash')">read from <strong>iframe</strong></button>
</div>
</body>
</html>

http://safari.oreilly.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-596-00467-2/jvdhtmlckbk-CHP-14-SECT-13&open=true&g=&catid=&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1
http://www.sitepoint.com/article/872



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum