View Full Version : calling a function in an iframe

04-07-2003, 04:55 AM
i want to call a function that is located within the javascript of an iframe. Can that be done like this?


04-07-2003, 05:02 AM
if (frames['iframeOut'].calc) frames['iframeOut'].calc();

04-07-2003, 05:21 AM
ty very much, can u explain it for me tho. I know how to put it in my code and stuff i just want to learn more about javascript, and the only way to do that is ask questions :)

04-07-2003, 06:16 AM
IFRAMES are schizo - half-element, half-window...:eek: so you need to keep in mind what you want to do with them before scripting. Get their window object to do 'window-like' things (call JS, change location) and their HTMLElement object to do 'element-like' things (CSS, e.g.). I'm still never sure what'll work. See if this confuses you:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<script type="text/javascript" language="javascript">

var HTML = '<script>function foo(){document.body.innerHTML+="foo<br />";}</'+'script>';

<iframe name="iframeOut" id="iframeOut" src="javascript&#58;parent.HTML"></iframe><br /><br />
<a href="javascript&#58;;" onclick="if (frames['iframeOut'].foo) frames['iframeOut'].foo()">call foo()</a><br /><br />
<a href="javascript&#58;;" onclick="alert(document.getElementById('iframeOut'))">
alert(document.getElementById('iframeOut')---<b>what does this method return?</b></a><br />
<a href="javascript&#58;;" onclick="alert(frames['iframeOut'])">
alert(frames['iframeOut'])---<b>what does this reference (window.frames[] array) return?</b></a><br />
<a href="javascript&#58;;" onclick="alert(document.getElementById('iframeOut') == frames['iframeOut'])">
alert(document.getElementById('iframeOut') == frames['iframeOut'])---<b>Are they the same object?</b></a><br /><br />
<a href="javascript&#58;;" onclick="alert(document.getElementById('iframeOut').tagName);
alert(frames['iframeOut'].tagName---<b>Which one is an element object?</b></a>

The src="javascript&#58;..." bit is just to avoid a second .htm file.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum