468,545 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

On fly addition of an onload event to an iframe document

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
Mar 12 '06 #1
7 9547
In article <MP***********************@news.tiscali.fr>, no@thanks.com
says...
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 ?
Mar 12 '06 #2

Asterbing wrote:
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") {
...
}
}

Mar 13 '06 #3
In article <11*********************@j33g2000cwa.googlegroups. com>,
ma***@ukr.net says...
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 ?
Mar 13 '06 #4
In article <11*********************@j33g2000cwa.googlegroups. com>,
ma***@ukr.net says...
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>
Mar 13 '06 #5
Asterbing wrote:
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.

Mar 13 '06 #6

Asterbing wrote:
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.

Mar 13 '06 #7
In article <11**********************@i39g2000cwa.googlegroups .com>,
ma***@ukr.net says...
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.
Mar 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Koms Bomb | last post: by
6 posts views Thread by pierre.bru | last post: by
5 posts views Thread by Andy Fish | last post: by
8 posts views Thread by niebo077 | last post: by
reply views Thread by dimepiece18 | last post: by
7 posts views Thread by Tom Cole | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.