Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-07-2003, 07:29 PM   PM User | #1
Endeavor
New to the CF scene

 
Join Date: Jul 2003
Location: The City of Saint Petersburg
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Endeavor is an unknown quantity at this point
<iframe> onload event

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
Endeavor is offline   Reply With Quote
Old 07-07-2003, 08:51 PM   PM User | #2
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
In the page loading the Iframe



document.myIFRAME.document.body.innerHTML


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


Last edited by Mr J; 07-08-2003 at 03:27 PM..
Mr J is offline   Reply With Quote
Old 07-08-2003, 05:37 AM   PM User | #3
Endeavor
New to the CF scene

 
Join Date: Jul 2003
Location: The City of Saint Petersburg
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Endeavor is an unknown quantity at this point
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
Endeavor is offline   Reply With Quote
Old 07-08-2003, 03:39 PM   PM User | #4
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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
Mr J is offline   Reply With Quote
Old 07-08-2003, 06:42 PM   PM User | #5
Endeavor
New to the CF scene

 
Join Date: Jul 2003
Location: The City of Saint Petersburg
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Endeavor is an unknown quantity at this point
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!..
Endeavor is offline   Reply With Quote
Old 07-08-2003, 08:05 PM   PM User | #6
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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
Attached Files
File Type: zip iframe.zip (2.7 KB, 4956 views)
Mr J is offline   Reply With Quote
Old 07-08-2003, 08:31 PM   PM User | #7
Endeavor
New to the CF scene

 
Join Date: Jul 2003
Location: The City of Saint Petersburg
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Endeavor is an unknown quantity at this point
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 is offline   Reply With Quote
Old 07-08-2003, 08:58 PM   PM User | #8
Endeavor
New to the CF scene

 
Join Date: Jul 2003
Location: The City of Saint Petersburg
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Endeavor is an unknown quantity at this point
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_??.."
Endeavor is offline   Reply With Quote
Old 07-08-2003, 09:34 PM   PM User | #9
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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



I don't usually code for NS but I thought I'd give it a try
Mr J is offline   Reply With Quote
Old 07-09-2003, 01:02 AM   PM User | #10
Endeavor
New to the CF scene

 
Join Date: Jul 2003
Location: The City of Saint Petersburg
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Endeavor is an unknown quantity at this point
..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 -->>
Endeavor is offline   Reply With Quote
Old 09-26-2006, 01:53 PM   PM User | #11
garmooshka
New to the CF scene

 
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
garmooshka is an unknown quantity at this point
That works great.

Is there a way i can get the html source of the iframe into a variable?
garmooshka is offline   Reply With Quote
Old 09-26-2006, 05:26 PM   PM User | #12
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
See if this helps

http://www.huntingground.freeserve.c...ad_content.htm
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 12-09-2006, 05:38 AM   PM User | #13
cainnech
New to the CF scene

 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
cainnech is an unknown quantity at this point
Quote:
Originally Posted by Mr J View Post
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!
cainnech is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:02 AM.


Advertisement
Log in to turn off these ads.