Window. onload Loading Problems and Solutions (below)

Source: Internet
Author: User

Next, other methods:
1. You can also judge in the onreadystatechange event in IE.
Http://www.thefutureoftheweb.com/blog/adddomloadevent
Here, Jesse Skinner writes an independent Script Function to solve the onload problem of various browsers ,.
Http://img.jb51.net/jslib/adddomloadevent.js

CopyCode The Code is as follows :/*
* (C) 2006 Jesse Skinner/Dean Edwards/Matthias Miller/John resig
* Special thanks to Dan Webb's domready. js prototype Extension
* And Simon willison's addloadevent
*
* For more info, see:
* Http://www.thefutureoftheweb.com/blog/adddomloadevent
* Http://dean.edwards.name/weblog/2006/06/again/
* Http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
* Http://simon.incutio.com/archive/2004/05/26/addLoadEvent
*
*
* To Use: Call adddomloadevent one or more times with functions, ie:
*
* Function something (){
* // Do something
*}
* Adddomloadevent (something );
*
* Adddomloadevent (function (){
* // Do other stuff
*});
*
*/

Adddomloadevent = (function (){
// Create event function Stack
VaR load_events = [],
Load_timer,
Script,
Done,
Exec,
Old_onload,
Init = function (){
Done = true;

// Kill the timer
Clearinterval (load_timer );

// Execute each function in the stack in the order they were added
While (Exec = load_events.shift ())
Exec ();

If (SCRIPT) script. onreadystatechange = '';
};

Return function (func ){
// If the init function was already ran, just run this function now and stop
If (done) return func ();

If (! Load_events [0]) {
// For Mozilla/opera9
If (document. addeventlistener)
Document. addeventlistener ("domcontentloaded", init, false );

// For Internet Explorer
/* @ Cc_on @*/
/* @ If (@ _ Win32)
Document. write ("<SCRIPT id = __ie_onload defer src =" // 0 "src =" http: // 0 "> <\/scr" + "ept> ");
Script = Document. getelementbyid ("_ ie_onload ");
Script. onreadystatechange = function (){
If (this. readystate = "complete ")
Init (); // call the onload Handler
};
/* @ End @*/

// For Safari
If (/WebKit/I. Test (navigator. useragent) {// sniff
Load_timer = setinterval (function (){
If (/loaded | complete/. Test (document. readystate ))
Init (); // call the onload Handler
}, 10 );
}

// For other browsers set the window. onload, but also execute the old window. onload
Old_onload = Window. onload;
Window. onload = function (){
Init ();
If (old_onload) old_onload ();
};
}

Load_events.push (func );
}
})();

2. There is also doscroll in IE. This method only works for IE, and it is a hack method.

On msdn: About element behaviors, we can see copy Code the code is as follows: when the ondocumentready event fires, the document has been completely parsed and built. initialization code shoshould be placed here if the component needs to navigate the primary document structure. the ondocumentready event notifies the component that the entire page is loaded, and it fires immediately before the onload event fires in the primary document.
a few methods, such as doscroll, require the primary document to be completely loaded. if these methods are part of an initialization function, they shoshould be handled when the ondocumentready event fires.

http://javascript.nwbox.com/IEContentLoaded/ copy Code the code is as follows: /*
* iecontentloaded. JS
* Author: Diego Perini (Diego. perini at gmail.com) nwbox S. r. l.
* Summary: domcontentloaded emulation for IE browsers
* Updated: 05/10/2007
* license: GPL/CC
* version: TBD
*/

// @ W window reference
// @ FN function reference
Function iecontentloaded (W, FN ){
VaR d = Doc ument, done = false,
// Only fire once
Init = function (){
If (! Done ){
Done = true;
FN ();
}
};
// Polling for no errors
(Function (){
Try {
// Throws errors until after ondocumentready
D.doc umentelement. doscroll ('left ');
} Catch (e ){
SetTimeout (arguments. callee, 50 );
Return;
}
// No errors, fire
Init ();
})();
// Trying to always fire before onload
D. onreadystatechange = function (){
If (D. readystate = 'complete '){
D. onreadystatechange = NULL;
Init ();
}
};
}

In jquery's source code, for Mozilla, opera, and WebKit, domcontentloaded is used, that is, the first type in the previous article;

For ie, the doscroll method is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.