Entry
How do I script an IFRAME?
How do I access the content of an IFRAME?
How to scroll iframe content from the parent frame?
Mar 19th, 2000 08:31
Martin Honnen, Frank S,
With full DOM access we have two separate ways for two different
purposes to script an IFRAME.
Every IFRAME is contained as a window object in the
window.frames
array (in both IE4+ and NN6 it seems), thus if you want to set the
location of an IFRAME or access its document and its content you
reference
window.frames.iframeName
and then
window.frames.iframeName.location.href
window.frames.iframeName.document.links
etc.
If you want to access the HTMLIFrameElement the IFRAME tag creates you
access it with standard DOM methods:
var iframeElement = document.getElementById('iframeId');
respectively
var iframeElement = document.all['iframeId']
for IE4 and then you can set the html attributes e.g.
iframeElement.width = 300;
The following contains examples of both uses: an empty IFRAME is
included in the page whose content is then change by getting the
iframe's window object and document.writing its document object. A
javascript link to access the iframe's content and another js link to
change the width of the iframe are provided.
<HTML>
<HEAD>
<SCRIPT>
var frameSrc = '';
frameSrc += '<HTML><BODY>';
frameSrc += '<BUTTON ONCLICK="alert(\'Kibology\');">';
frameSrc += 'button<\/BUTTON>';
frameSrc += '<\/BODY><\/HTML>';
var iframeWin, iframeElement;
function init () {
iframeWin = window.frames.anIframe;
iframeWin.document.open();
iframeWin.document.write(frameSrc);
iframeWin.document.close();
iframeElement =
document.getElementById ?
document.getElementById('anIframe') :
document.all['anIframe'];
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="init();">
<A HREF="javascript: alert
(iframeWin.document.body.firstChild.nodeName); void 0">
show iframe first child
</A>
|
<A HREF="javascript: iframeElement.width = 300; void 0">
set width to 300
</A>
<BR>
<IFRAME ID="anIframe" NAME="anIframe" SRC="about:blank" WIDTH="100">
</IFRAME>
</BODY>
</HTML>