DOMready in javascriptIE _ javascript skills

Source: Internet
Author: User
When we want to execute a function after page loading, we will surely think of onload. But in the browser's view, onload can only happen after all the things on the page are loaded, but it's too late. If you only need to perform operations on the DOM, there is no need to wait until all the pages are loaded. We need a faster method.
Firefox has a DOMContentLoaded event that can be easily solved. Unfortunately, IE does not.
MSDN's method of JSCRIPT is unremarkable. When the page DOM is not loaded, an exception occurs when the doScroll method is called. If no exception occurs, the page DOM is loaded!

The Code is as follows:

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 );
// No errors, fire
Init ();
// Trying to always fire before onload
D. onreadystatechange = function (){
If (d. readyState = 'complete '){
D. onreadystatechange = null;
Init ();

This function was released by Diego Perini in,
It is also widely recognized that many open-source frameworks use this method, such as ready in JQuery.

If you need to use IE's DomReady later, it will be him.

IEContentLoaded (document. getElementById ("test"), test );

Function test (){}
Related Article

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: 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.