HTML load and script run, because the HTML is not fully loaded and the script cannot find the DOM element cannot execute the event

Source: Internet
Author: User
Tags sleep function

Questions :

Title, that is, after alert appears a dialog box, I do not point off, the other JS file or HTML will be loaded, I encountered a problem is to invoke a DIV Click event when the page is loaded, but not executed, but before the call event to add alert (' xx ') After running, manually close the prompt box click event to invoke Success, why, also write a sleep function, but no matter how long the click event is not executed, how can I automatically invoke this click.

Workaround :

The code is included in the OnLoad and is loaded (Window.onload ()).

principle :

The first page of the code you write the order of loading, of course, sometimes due to the network cause the order of loading success is different, but the basic will not be too much difference.

It loads your vote[0], and then this vote[0] points to the DOM (that is, your div) is not loaded and parsed, and the browser does not yet know what this vote[0] is.

So, this time the browser will error this line of code, if you use the browser's F12 function key to open the debugger, you should be able to see the error message.

If you want this code to run directly when it is loaded, consider the order of execution, either onload executes it or put it behind HTML.

And why is it easy to use after alert? Because the alert execution time, waiting for the user to confirm the process, the program is blocked, do not go down, but the page loading process is not aborted, which is equivalent to the page loading and parsing time, was alert to stay, if the user's deft on hand, and the network speed is very slow, the same will be error, does not execute.

Reference page: Http://zhidao.baidu.com/link?url=fYzeXBFJfICTdMUt6-I2UU7VmiBX2-Dz_NEtaQc6-YuG56mfoslMaRORBJtGcZoZPfW_ Etuf0ckqrhqsgejp_ja4o3pcaj8z5zgekgqhypk

JS load execution order in HTML: http://www.cnblogs.com/lindaWei/archive/2012/04/05/2433454.html http://www.jb51.net/article/36755.htm

Execution of OnLoad and jquery $ (function () {}): http://lgscofield.iteye.com/blog/1884352

JS and C # methods call each other: http://www.cnblogs.com/lindaWei/archive/2012/04/05/2433385.html

HTML load and script run, because the HTML is not fully loaded and the script cannot find the DOM element cannot execute the event

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.