Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect if a page has completely loaded? (I can't use onLoad!)

Status
Not open for further replies.

zfs

Programmer
Joined
Feb 23, 2006
Messages
7
Location
EE
The problem: I need to dynamically assign some params for elements (input fields) on a page when it loads. But I can't do it before the elements are actually there (page has been loaded).

I can't use window.onLoad or <body onLoad="func();"> because I have no access to the <head> part of pages (framework issue). So my altering functions are included in the body part of page.

I tried to use this function:
Code:
function checkLoading(){
    if (document.body)
        eval("myFunction()\;");
    else
        setTimeout("checkLoading()", 1);
}
Sadly it doesn't avoid the "racing condition" and thus doesn't work for bigger (larger) pages :( I can get the same function to work if I add an additional timeout like that:
Code:
function checkLoading(){
    if (document.body)
        [b]setTimeout("myFunction()", 100);[/b]
    else
        setTimeout("checkLoading()", 1);
}
But thats not a good solution and may fail with even bigger pages...

I think that my approach to keep checking until something is done, is not bad, but maybe I could/should check something else instead of
Code:
if (document.body)

Any ideas?
 
Just put the script access to the elements toward the bottom of the page from where the elements are. You can add the <script> right after the </form> tag and it should work fine.

Lee
 
I can't use window.onLoad or <body onLoad="func();"> because I have no access to the <head> part of pages

Just because you have no access to the head section, does not mean you cannot use onload. Simply put your script tag, setting onload, in whatever part of the page you do have acess to:

Code:
<p>some html in the body...</p>

<script type="text/javascript">

   onload = function() {
      // do some stuff here...
   }

</script>

<p>yet more html</p>

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Somehow I thought I can't set onLoad in body, because the body is already loaded when the code reaches to the point I'm tring to set onLoad . Abviously thats not the case and ofcourse I can set onLoad in body... So I used the function described in this FAQ and it seems to work just fine.

Thanks to all, and sry for my rather dumb question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top