Javascript cannot convert to object

Source: Internet
Author: User

I recently encountered this problem when I was learning JavaScript. First paste the code

 1 

Operabrowser is used for testing. In the opera console, an error "cannot convert 'document. form1 'to object" is thrown ".

However, if you put Javascript in the body, there is no problem. JS is placed in the body and the head is different. The former is to load and execute JS, while the latter is just to load.

The solution is simple:

1. var txt0000document.form1.txt; put it in the body.

2. var txt0000document.form1.txt; put it in the function.

Another problem exists here. You can add an id = 'test' to the text box and use VaR TXT = Document. getelementbyid ('test'); to replace the original code. In this way, the console will not report errors. Why does document. form1 fail while document. getelementbyid () does?

To be continued .....

I just got back from Beijing after my work.

I searched Google for half a day and did not find the answer. Then I tested it with IE and chrome. VaR TXT = Document. getelementbyid ('test'); is not allowed. So try not to write this global variable in the head. js in the head only loads but does not execute it, and it is different in the body. Load and execute.

The difference between JS head and body http://www.irt.org/script/421.htm

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.