On fly addition of an onload event to an iframe document

A

Asterbing

Hi,

Talking about a js script which changes an iframe src through a "ref_to
iframe.setAttribute("src", document_path);", I would like to launch a
check() fct when this new document is loaded.

Of course, knowing, I don't want (and can't n some cases where document
is generated by a cgi script) to edit every possible document which is
potentially loadable in the iframe.

In fact, I've through about the idea to on fly add an "onload='check
()';" to every document, but don't know how to do that :-(

Maybe using attachEvent or something arounnd this : I don't know. Of
course, I wish a solution working in the majors browsers.

How to do ?
Thanks in advance
 
A

Asterbing

Talking about a js script which changes an iframe src through a "ref_to
iframe.setAttribute("src", document_path);", I would like to launch a
check() fct when this new document is loaded.

Close to be solved with a call to this fct after src change :

function wait4newdoc()
{
if (ref_to_iframe.document.readyState == "complete"){
check();}
else{
setTimeout("wait4newdoc()",50);}
}

But it doesn't work everytime. Maybe a problem about readyState ?
 
M

marss

Asterbing said:
Close to be solved with a call to this fct after src change :

function wait4newdoc()
{
if (ref_to_iframe.document.readyState == "complete"){
check();}
else{
setTimeout("wait4newdoc()",50);}
}

But it doesn't work everytime. Maybe a problem about readyState ?

MSDN states: "The readyState property enables the status of an object
to be tested. The correct place to test the readyState property is in
the event handler for onreadystatechange..."
Try this:

document.onreadystatechange=foo;

function foo(){
if (document.readyState=="complete") {
...
}
}
 
A

Asterbing

document.onreadystatechange=foo;

When you talk about document, do you talk about window.document or
ref_to_iframe.document.

If this is the second one, it means I have to modify every document
which is loadable inside the iframe ? Do I have to go through something
like attachEvent or else ?

How to do this on fly, knowing every document is dynamically generated
from a cgi script I can't modify ?
 
A

Asterbing

MSDN states: "The readyState property enables the status of an object
to be tested. The correct place to test the readyState property is in
the event handler for onreadystatechange..."
Try this:

Well, to be more accurate, I've created a page which characterize the
problem. Just copy/paste. Here it is :

<html>
<head>
<title>Check Statistics</title>
<script type="text/javascript" language="JavaScript"><!--
var f = document.getElementById("stats");

function WaitStats4Checking(){
if (f.document.readyState == "complete"){Check();}
else{setTimeout("WaitStats4Checking()",50);}}

function GetStats(){
var f = document.getElementById("stats");
f.setAttribute("src","/cgi-bin/getstats.exe");
WaitStats4Checking();}
//--></script>
</head>
<body>
<p>Initially, the iframe contains help.htm which just says "Not any
statistic loaded. Click 'Get Statistics'" for which a heigh of 20px is
enough.
<br><br>
When user click on the 'Get Statistics' link, the GetStats() javascript
function run the getstats.exe CGI script which returns a generated
document in iframe.
<br><br>
When this generated document is well loaded in iframe (not before), the
Check() javascript function should be launched.
<br><br>
Current problem is that (f.document.readyState == "complete") is always
true even if generated document is not fully loaded.
<br><br>
How to solve this ? Maybe going through onreadystatechange to launch
WaitStats4Checking() rather than at the end of GetStats(), but how ?</p>

<a href="#" onclick="GetStats();">Get Statistics</a>

<div style="position: absolute; visibility: visible; top: 300px; left:
15px; height: auto; width: 731px; padding: 5px; overflow: hidden;
background: white; color: #000000">
<iframe id="stats" width=720 height=20 src="help.htm" frameborder="yes"
scrolling="no"></iframe>
</div>
</body></html>
 
M

marss

Asterbing said:
var f = document.getElementById("stats"); ....
function GetStats(){
var f = document.getElementById("stats");
....

1.You used the same name for both local and global variables.
2.No need to use timeout to check readyState, WaitStats4Checking is
called when state of document loaded in the iframe changed.

Next code has to work:

<script type="text/javascript" language="JavaScript"><!--
function WaitStats4Checking()
{
var f = document.getElementById("stats");
if (f.readyState == "complete")
{
Check();
}
}

function GetStats()
{
var f = document.getElementById("stats");
f.setAttribute("src","http://google.com");
f.onreadystatechange = WaitStats4Checking;
}

function Check()
{
alert("Check");
}

//--></script>

Sorry for my English.
 
A

Asterbing

Next code has to work:

Hum, I've asked through another approach in the "How to detect iframe's
doc change..." thread and been branched to the onload="Check();" way...
It sounds to work, unless the fact it doesn't work in all browsers. If I
don't succeed his way, I'll be back to your code. Thanks again, marss.
 
M

marss

Asterbing said:
var f = document.getElementById("stats"); ....
function GetStats(){
var f = document.getElementById("stats");
....

You use the same name for both local and global variables.

And no need to use timeout to check readyState, WaitStats4Checking is
called when state of document loaded in the iframe changed.
Next code has to work:

<script type="text/javascript" language="JavaScript"><!--
function WaitStats4Checking()
{
var f = document.getElementById("stats");
if (f.readyState == "complete")
{
Check();
}
}

function GetStats()
{
var f = document.getElementById("stats");
f.setAttribute("src","http://google.com");
f.onreadystatechange = WaitStats4Checking;
}

function Check()
{
alert("Check");
}

//--></script>

Sorry for my English.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top