서지혜
서지혜

Reputation: 17

How can I include this inside jQuery instead of inside body tag?

<body onload="onDocLoad()" onresize="onResize()">

I would like my webpage and JavaScript to do the same thing,

but I want to include those function calls inside jQuery source.

Is it

$( document ).ready( onDocLoad ).resize(onResize); 

Thanks a lot, Stack Overflow!

Upvotes: 1

Views: 42

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074188

Close. An exact replacement would be:

$(window).on("load", onDocLoad).on("resize", onResize);

That waits for the window load event before the first call to onDocLoad

You may be able to do this instead:

$(document).ready(function() {
    onDocLoad();
    $(window).on("resize", onResize); 
});

Or even this, with the script tag at the end of the document (just before the closing </body> tag):

(function() {
    onDocLoad();
    $(window).on("resize", onResize); 
})();

It depends on whether you need to wait for all resources to load before the first call to onDocLoad. If you do, use the first example above (which waits for the window load event). If you don't, either of the second examples makes the first call to onDocLoad sooner, before images and other resources have finished loading.

Upvotes: 2

Related Questions