5416339
5416339

Reputation: 417

How to reduce delay of "Onload"?

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

Upvotes: 2

Views: 2524

Answers (3)

Quentin
Quentin

Reputation: 943547

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)

Upvotes: 6

David Hedlund
David Hedlund

Reputation: 129792

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.

Upvotes: 4

Tilman Koester
Tilman Koester

Reputation: 1739

Obvious answer: Speed up your page load.

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

Upvotes: 0

Related Questions