Talk about my JS learning process (ii)--"Hello world!"

Source: Internet
Author: User

In the "Talk about my JS learning process (a)", I briefly talked about what I think of JavaScript learning methods, then we can try to write a simple JS code.

"Hello world!" For me the meaning is really significant, for example when you see a pop-up window beautifully elegant (=. = ) to pop out of the browser and bring your own Hello world!

That sense of accomplishment will leave you with the belief that you can learn the language well. Of course, that is the case. No more nonsense ...

We do not use JavaScript to specifically install additional components, do not configure any path, and so on, you can run directly in the browser. Most basic, what we need to do is add a script block to the. html document. As for the location of this code block, I'll say a little bit, now add this block of code to the

Code

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >

The script code in this page has only one row, which pops up an alert dialog box.

In this code, the JavaScript code is encapsulated within the <script></script> element, and when the browser reads this element, it does not parse the code in HTML or XHTML, Instead, it informs the browser's scripting engine (JS interpreter) to manage the contents of this area. The script embedded in the Web page does not necessarily refer to the type attribute of the Javascript,script element, which defines the kind of script, the type of specimen defined in this example is Text/javascript. There are other possible properties, such as

For other properties of script, you can expand your learning by referring to the content here.

Http://www.w3school.com.cn/tags/tag_script.asp

" about the location of JavaScript code "

First of all, JavaScript can be placed anywhere in the HTML document, but since the browser is parsing the page structure and information from top to down in the document flow, placing it in a different location will have different effects on parsing, or it will have a certain effect on JavaScript execution.

There are several different ways to refer to JS in a Web page:

The first most common is embedding JavaScript code inside the head tag;

The second is to put the JS code inside the <body></body> tag;

The third is to refer to the external. js file, which is written in

1) defined in the head element of the JS code, will be loaded in the entire page before the JS, generally recommended to write in this position, although there is always a problem is, if you want to get an HTML element in JS, but this time the HTML is not loaded, easy to appear JS can not find the corresponding element of the problem, So we will use window.onload = function () {}, the part of the JS code is written in this anonymous function, you can, after the page is fully loaded, then execute window.onload = function () {} inside the JS statement.

2) defined in the BODY element of the JS code, is loaded according to the flow of the document, when the JS code encountered, the browser will stop the HTML, CSS and pictures, such as loading, will be JS loading completed before continuing.

3) referring to the external JS file, is a content structure separation of the practice, when the JS code is more than the time, you can write the JS code in a. js file, and then use the <script/> Tag reference in.

(about the location of JS, this problem involves more content, such as the rendering of the page effect, whether the order to modify the DOM elements and so on, this part of the discussion after the separate, here is recommended a blog, about JS loading problem

Http://www.blogjava.net/BearRui/archive/2010/04/08/web_performance_js_where.html Bearrui (AK-47) 's blog)

Talk about my JS learning process (ii)--"Hello world!"

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.