Window.onload Loading problem and solution (next) _javascript skills

Source: Internet
Author: User

Next, other methods:
First, in IE can also be in the onReadyStateChange event to judge
Http://www.thefutureoftheweb.com/blog/adddomloadevent
Here's Jesse Skinner wrote a separate script function to solve the onload problem of various browsers.
Http://img.jb51.net/jslib/adddomloadevent.js

Copy Code code as follows:

/*
* (c) 2006 Jesse Skinner/dean Edwards/matthias Miller/john resig
* Special to Dan Webb ' s domready.js Prototype extension
* and Simon Willison ' s addloadevent
*
* For more info, the following:
* 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 is already ran, the just run this function is 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 "&GT;&LT;\/SCR" + "ipt>");
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 the 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);
}
})();

Second, there are doscroll in IE, this is a method only for IE, and it is a hack method.

In Msdn:about Element behaviors we can see
Copy Code code as follows:

When the Ondocumentready event fires, the document has been completely parsed and built. Initialization code should is placed here if the component needs to navigate the primary document structure. The Ondocumentready event notifies the component, the entire page is loaded, and it fires immediately the before D 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 the initialization function, they should is handled when the Ondocumentready event fires.

http://javascript.nwbox.com/IEContentLoaded/
Copy Code code 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 = w.document, done = False,
Only Fire once
init = function () {
if (!done) {
Done = true;
FN ();
}
};
Polling for no errors
(function () {
try {
Throws errors until after Ondocumentready
D.documentelement.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 the jquery source, for Mozilla, Opera and WebKit use is domcontentloaded, that is, the first of the previous article;

And for IE use is the DoScroll method.

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.