views:

44

answers:

3

I'm current using a javascript with the this tag

<body onload="javascript:Mine();">

Now i noticed that it is taking some time to load this function after i open the page.So is there any other way i can reduce this delay ?

Thanks

A: 

Obvious answer: Speed up your page load.

onLoad means, that function will be run after your whole page has finished loading.

tilman
Downvote! Ouch. Sorry, after reading it again, it does sound condescending. Didn't mean to.
tilman
CAn anyone explain in simpler terms ?
5416339
David Dorward did an excellent job, check out his answer.
tilman
+4  A: 

OnLoad waits for all images and other external resources to be completely loaded. If you only want to know that the entire DOM tree with all HTML elements have been loaded, use OnDOMReady.

Note that this is not a trivial task if you want it to work well across many browsers. If you're using jQuery, then they've solved the problem for you, and you can write:

$(document).ready(function() {
    Mine();
});

But if not, loading jQuery only for that feature might not improve your page load at all. Another solution, then, would be to put the call just before </body>:

<body>
    ...
    <script type="text/javascript">Mine();</script>
</body>

Most of your DOM tree should be available to you at that point, so that might be a way to go.

David Hedlund
I use JQuery approach very often. If you do not know JQuery, it definitively worth it.
Luc
+5  A: 

onload fires when a page is completely loaded. To get things to fire sooner you can either:

  • Make the page load faster
    • Remove, reduce and optimize images (and other content, third party adverts are often a major performance kicker)
    • Follow performance guidelines (such as those from Yahoo!.)
  • Not use onload
    • Use a library (such as YUI or jQuery) that provides a domready event
    • Run the script in a <script> directly (instead of assigning an event handler)
David Dorward