"JavaScript" appears even if you set the ID and can not get to the probable cause with window.onload

Source: Internet
Author: User

Sometimes. In JavaScript. Even if you set the ID it is possible that document.getElementById () will not get the situation, and then you start to think that the document is wrong or something. In fact, it's not your code. There is a problem with uppercase and lowercase. The main thing is that you haven't figured out exactly where the difference is between what's written directly in <script></script> and what's written in Window.onload=function () {}.

For example, the following is a very simple code, in the Web page in addition to an ID of a, and then wrote the AAAA layer Div is nothing else, in the head there is a section written "very normative" script, the <script></script> will be able to , with <script type= "Text/javascript" language= "JavaScript" ></script> there is no difference at all. Lest some people think of themselves as a disciplined flow of discipline. This is the cause of the following series of problems.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


The script first takes a layer and then gets the content of a layer after window.onload. Expect very many people to think that the first line of code

Alert (document.getElementById ("a"));
Would pop up something like [object:html objectelement]? I felt the same way before, but the results were actually executed. It is for example the following effect:


Hey? Why is the first alert going to pop empty? This is due to the fact that HTML is compiled one line at a time when it is loaded. Dynamically compiled, it does not compile the entire program at once, as in the case of the C language. I'll give you the execution. This is the difference between the so-called dynamic compilation domain and the static compilation domain. Therefore, it is possible to explain why JavaScript reads the error itself and stops executing. There will also be problems with the "JavaScript" variable conflicts that I've mentioned earlier that can be compiled (click to open a link).

Get back to the chase. Start, when the Web page reads the first alert script. A layer with ID A does not appear in the Web page at all div!

So it is very normal not to get it.

The solution has two, one is to use Window.onload=function () {}, which requires that the script must be executed at the end of the page. Two is. Put this script directly in the following

Of course, through this example, you also need to be clear, assuming that the introduction of JavaScript in the head. Similar to <script src= "Js/jquery-1.11.1.js" ></script> the like, note. Assuming that JavaScript is introduced in JavaScript that will be loaded at the beginning of the page, you must write the script to Window.onload=function () {}

"JavaScript" appears even if you set the ID and can not get to the probable cause with window.onload

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