...

View Full Version : <iframe> onload event



Endeavor
07-07-2003, 07:29 PM
Gentlemen! Would anybody tell, please..
<iframe name="myIFRAME"></iframe>
<script>
myIFRAME.document.onload = alert(someStuff);
</script>
-- is it a right call to fire the onload event? I can't get the innerHTML of the <iframe> using this syntax (and lots other).
How to get the innerHTML of a page loaded into the iframe??

Lots of _GRATITUDE_ for all!!!

Respectfully,
Endeavor

Mr J
07-07-2003, 08:51 PM
In the page loading the Iframe



document.myIFRAME.document.body.innerHTML


document.myIFRAME.document.getElementById ("your_id").innerHTML

Endeavor
07-08-2003, 05:37 AM
Thank You very much Mr J!
Well the root of the problem is that _possibility_ to get the valid innerHTML of an <iframe>.
Here goes:

<script>
//the urls array I try to use
URLs = [null, 'outer.html', ... url2, url3, ...];
//the loader function
function loadOuterContent(theURL){
nestFRAME.location.href = theURL;
rootDIV.innerHTML = nestFRAME.document.body.innerHTML;
}
</script>
<div id="myButton" onclick="loadOuterContent(URLs[1])">button</div>
<iframe name="nestFRAME"></iframe>
<div id="rootDIV"> </div>

>> event #_
1.) By firing URLs[1] the <iframe> gets URLs[1], and rootDIV gets NOTHING;
2.) by url2 (the URLs array) I get that damn URLs[1] in my miserable rootDIV instead of the very url2 (that is handled properly by the nestFRAME though);
3.) By url#...12345 the <iframe> gets it, and the rootDIV receives, on its turn, the last iframe's content, that's url2.
Etc., rootDIV goes one step behind of the nestFRAME.

All pointed above points at that there's a lack of coincidence in the history steps between <iframe> and the rootDIV.
So I decided to fire the 'onload' event of the <iframe> object to swap the rootDIV's content with the _LOADED_ iframe's _PROPER_ one. But it seems it didn't help.. :( Was the 'iframe onload' syntax right again?, what should I do (instead of getting some beer :)??

Thanks a lot again!!
G'luck!

>> Respectfully,
>> Endeavor

Mr J
07-08-2003, 03:39 PM
I think that the problem here is that the line

rootDIV.innerHTML = nestFRAME.document.body.innerHTML

is run before the document has finished loading in the Irame

if you put this line on a timeout then it should work.

setTimeout("rootDIV.innerHTML = nestFRAME.document.body.innerHTML",2000)

I have tried this out and without the timeout I got nothing showing in rootDIV.

With the timeout I got the contents of the page I loaded into the Iframe

Endeavor
07-08-2003, 06:42 PM
Thank you so much sir!
Yes, apparently browser holds an old iframe's content while reloading until onload event occurs.
But will setting timeout solve the problem, actually?
It appears that 'onload' event must be more correct because it patently tells browser to check if a proper document really loaded, and if true --> perform requested action, else --> still wait.

In case of timeout browser simply looks at what is placed in the iframe after pointed time, and then goes work, no matter was the timeout enough to load the next document.
So if there's a poor dial-up modem connection, poor line etc., a user, it seems, must receive an old page again -- a brouser will give what it keeps loaded.
Maybe I'm wrong, I'm just a newbie in JS. I was trying to use 'onload' but it doesn't work whatever I performed to fire it. :(

There's a sample code from here down:
_________________________________

<html><head><title>iframe-to-div sample</title>

<style type="text/css"><!--
body{overflow:hidden}
.rootDiv{border:1px solid slategray; cursor:hand; font-weight:bold; font-size:14px; letter-spacing:1px; font-family:Verdana;}
.nest{position:absolute; margin-top:34px; left:23px; overflow:auto; width:123px; height:123px;}
--></style>

<script language="JavaScript1.2" type="text/javascript"><!-- //

//urls array..
URLs = [null, 'url_1.html', 'url_2.html', 'url_3.html', 'url_4.html', 'url_5.html'];

//text for buttons
txt=[null, "url_1", "url_2", "url_3", "url_4", "url_5"];

//take iframe's innerHTML and place it into DIV
function iframeToDiv()
{
screenDIV.innerHTML = ifr.document.body.innerHTML;
}

//load a proper url into iframe;
//then call the iframeToDiv() function when _onload_
function loadURL( theURL )
{
ifr.location.href = theURL;
ifr.document.onload = iframeToDiv();
}

//write menus with 'onclick' event..
function doStuff()
{
for(i=1; i<=5; i++)
{
document.write('<div class="rootDiv" id="'+i+'" onclick="loadURL(URLs[' + i + '])">'+txt[i]+ '</div><br />');
}
}

doStuff();

// --></script>
</head>
<body>

<!-- the hidden iframe where we load files for retreiving their innerHTML property -->
<iframe name="ifr" id="nestFRAME" class="nest"></iframe>

<!-- our brave div to take the innerHTML of documents _loaded_ into iframe -->
<div class="nest" id="screenDIV" style="left:180px; border:1px solid black"> </div>

</body></html>
_________________________________

And a little addition -- the sample files sources, please:

______________________

<!-- url_1.html -->

<html>
<head>
<title>url_1</title>
</head>
<body>
<p>
url_1
</p>
</body>
</html>
______________________

<!-- url_2.html -->

<html>
<head>
<title>url_2</title>
</head>
<body>
<p>
url_2
</p>
</body>
</html>
______________________

<!-- url_3.html -->

<html>
<head>
<title>url_3</title>
</head>
<body>
<p>
url_3
</p>
</body>
</html>
______________________

<!-- url_4.html -->

<html>
<head>
<title>url_4</title>
</head>
<body>
<p>
url_4
</p>
</body>
</html>
______________________

<!-- url_5.html -->

<html>
<head>
<title>url_5</title>
</head>
<body>
<p>
url_5
</p>
</body>
</html>
______________________

THANK YOU AGAIN, MR J!
Best regards --
>> Endeavor

p.s. Sorry for such overflood here!..

Mr J
07-08-2003, 08:05 PM
Yes I see what you mean.


You could have the page loading into the Iframe run the function when it has loaded.


This can be done in one of two ways.

1) Run the function in the page loaded into the Iframe onload and have it targeting rootDIV in the parent frame.

2) Run a function onload in the page loaded into the Iframe and have that function run the function in the parent frame.

Take a look at the example in the zip I have posted

Endeavor
07-08-2003, 08:31 PM
Of course. I simply put this line: 'onload="iframeToDiv()"' into my iframe's tag -- everything's fine, all works properly, done.
But how on Earth to get the onload event from SCRIPT, and then trigger iframeToDiv() function from _there_??..

_THANK YOU, THANK YOU, THANK YOU.

>> Oddie Endeavor :)

Endeavor
07-08-2003, 08:58 PM
Sorry, the last message I sent before watching if there a message from you.
And thanks again -- I got your attachment. I feel like it's a b'day now :)
Being a newbie it's hard to find a simple solution :( so sorry for that heap of questions. I do hope I didn't make a trouble or smth like..

Are you a coder, is there a home page of you?

Be lucky! Respectfully --
>> Endeavor

p.s.
"But how on Earth to get the onload event from SCRIPT, and then trigger iframeToDiv() function from _there_??.."

Mr J
07-08-2003, 09:34 PM
I have a website of sorts that you may look at.


www.huntingground.freeserve.co.uk


I am in the process of upgrading my scripts to accomodate NS7, a slow and very painful process

:D

I don't usually code for NS but I thought I'd give it a try

Endeavor
07-09-2003, 01:02 AM
..By the by, in IE4 the onload event seems unsupported at all (alerts don't work at least).
Well, is there some other way to explain a browser that an iframe's content is refreshed and loading is complete??

Thanks!
--- ENDEAVOR -->>

garmooshka
09-26-2006, 01:53 PM
That works great.

Is there a way i can get the html source of the iframe into a variable?

Mr J
09-26-2006, 05:26 PM
See if this helps

www.huntingground.freeserve.co.uk/webplus/iframes/iframe_read_content.htm

cainnech
12-09-2006, 05:38 AM
See if this helps

www.huntingground.freeserve.co.uk/webplus/iframes/iframe_read_content.htm

Well it did it for me.

However my problem isn't solved yet.
I'm trying to create a monitoring tool for texts beying displayed on a website through an external asp-page.

So first thing I'm trying to do is get the text out of the iframe and using the script of Mr J that already works perfectly on an html page (see: http://www.lookandlisten.be/test3.htm) but when I change it to an ASP-page it won't work anymore (check: http://www.lookandlisten.be/test4.htm).

I don't understand it because the output of the asp-page is plain html.

Anybody has an idea how this can be solved?

Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum