How do you add functions to $(window).load()?
Just curious if there's an easy way to add functions to the $(window).load()
event before it has fired. For example, if you call $(window).load()
twice in the beginning o开发者_运维技巧f the page, only the function of the second call will execute onload
.
Is there some sort of tool built into jQuery for adding to the onload
event instead of replacing it? If so, how about for the $(document).ready()
call?
They actually do stack in the order specified. Here's an example : http://jsfiddle.net/73D9Z/
I've used window.ready()
$(window).ready(function(){
alert('window ready 1');
});
$(window).ready(function(){
alert('window ready 2');
});
$(document).ready(function(){
alert('document ready 1');
});
$(document).ready(function(){
alert('document ready 2');
});
function windowLoad(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
function documentReady(func) {
var oldonload = document.ready;
if (typeof document.ready != 'function') {
document.ready = func;
} else {
document.ready = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
$(window).load() gets executed after a page is rendered.
$(document).ready(handler) executes the function passed as parameter, after the DOM is ready and before the page is rendered.
精彩评论