Timing of javascript execution

Last post 05-23-2008 8:25 AM by NC01. 8 replies.

Sort Posts:

  • Timing of javascript execution

    05-22-2008, 11:45 AM
    • Loading...
    • Zeevik
    • Joined on 05-21-2008, 6:31 PM
    • Posts 6

    Hello,

    Is there a way to force a javascript to execute at certain point in time during page rendering/loading?

    i.e.: let's say I want the script to run only after the page is fully loaded/rendered. Or, I want it to run only after a table is fully loaded. etc.

    Thanks. 

  • Re: Timing of javascript execution

    05-22-2008, 11:51 AM
    • Loading...
    • jamesqua
    • Joined on 08-03-2004, 6:55 PM
    • Columbus, OH
    • Posts 1,111
    You can use the onload event to fire the JavaScript after an object has loaded.
  • Re: Timing of javascript execution

    05-22-2008, 11:57 AM
    • Loading...
    • Zeevik
    • Joined on 05-21-2008, 6:31 PM
    • Posts 6

    Yes, I am aware of "onload", but from what I know using it with "body" doesn't guarantee that the page is fully loaded.

    And also it is supported only by: <body>, <frame>, <frameset>, <iframe>, <img>, <link>, <script>. (at least according to w3schools)

     

  • Re: Timing of javascript execution

    05-22-2008, 12:06 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 7,810
    • TrustedFriends-MVPs

    <script type="text/javascript">
    <!--
    window.onload = function ()
    {
     // Put the code here
    }
    // -->
    </script>

    NC...

  • Re: Timing of javascript execution

    05-22-2008, 12:22 PM
    • Loading...
    • Zeevik
    • Joined on 05-21-2008, 6:31 PM
    • Posts 6

    Are you saying it's guaranteed to run only when the page is fully loaded?

    Also, will it replace any other scripts/functions that are currently hooked up to the window onload event? If so, is there a way to append instead of replacing?

  • Re: Timing of javascript execution

    05-22-2008, 12:41 PM
    Answer
    • Loading...
    • jamesqua
    • Joined on 08-03-2004, 6:55 PM
    • Columbus, OH
    • Posts 1,111
    The answer to your second question is yes.  It will replace any other functions hooked to that event.  Here is a way to attach multiple events. Perhaps there is a better way to go about this.  What exactly are you trying to achieve?
  • Re: Timing of javascript execution

    05-22-2008, 12:41 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 7,810
    • TrustedFriends-MVPs

    Zeevik:

    Are you saying it's guaranteed to run only when the page is fully loaded?

    Surprise What are you going to sue me if it doesn't? There is no way to guarantee of that. That is just not the way browsers work.

    Zeevik:

    Also, will it replace any other scripts/functions that are currently hooked up to the window onload event? If so, is there a way to append instead of replacing?

    Yes it will replace any other scripts/functions that are currently hooked up to the window onload event. So just put all other scripts/functions that you want in the onload in there.

    NC...

  • Re: Timing of javascript execution

    05-22-2008, 1:07 PM
    • Loading...
    • Zeevik
    • Joined on 05-21-2008, 6:31 PM
    • Posts 6

    jamesqua:
    The answer to your second question is yes.  It will replace any other functions hooked to that event.  Here is a way to attach multiple events. Perhaps there is a better way to go about this.  What exactly are you trying to achieve?
     

     Thanks, this is exactly the answer to what I was asking about.

     

    NC01:

    Surprise What are you going to sue me if it doesn't? There is no way to guarantee of that. That is just not the way browsers work.

    No, I'm not american - I'm not into sueing. Wink The reason I was asking if "you guarantee" is because I heard from several people that attaching a script to the onload event doesn't guarantee that the page (with all its elements) is going to be fully loaded by the time the script executes. So rephrasing the "you guarantee" would be "does it usually/always work, from your experience?"

     

    NC01:

    So just put all other scripts/functions that you want in the onload in there.

    Unfortunately, that is not always possible Sad For example, in my case, I'm dealing with rather large e-commerce application that uses xml templates to generate web pages.

  • Re: Timing of javascript execution

    05-23-2008, 8:25 AM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 7,810
    • TrustedFriends-MVPs

    In my experience, attaching a process to the window.onload event has always worked.

    NC...

Page 1 of 1 (9 items)