9 JavaScript development best practices

Source: Internet
Author: User
Tags variable scope

1. Use the correct <script> tag
If you need to use some JavaScript code in html documents, you should usually use the following <script> tag:

<Script type = "text/javascript">
// Insert your code here
</Script> however, in the source code, the following tag syntax is everywhere:

<Script type = "text/javascript" language = "javascript">
// Insert your code here
</Script> in HTML, the language attribute is out of date because it is redundant when the type attribute is available. Don't write this again ......

In fact, the client specifies the type = "text/JavaScript" attribute for javascript code by default, unless the type = "type =" application/x-javascript "is required, otherwise, there is no need to write the type attribute. For JavaScript MIME types, here is a reference: http://krijnhoetmer.nl/stuff/javascript/mime-types/

2. Place the code in an external file
Using External. js documents is simpler than writing JavaScript code directly in html documents. It also allows JavaScript files to be cached to ensure quick access to website resources.

Place your JavaScript code in A. js document and use the <script> label in the html document to introduce it:

<Script src = "http://www.mangguo.org/myscript.js"> </script> 3. Avoid wrapping code with HTML annotations
In 1990s, some very old browsers were unable to execute JavaScript code. To prevent unnecessary results from these browsers. From 1994 to 1997, using annotation to wrap JavaScript code in HTML is a good compatibility solution to ensure that browsers that do not support JavaScript can ignore it.

Here is a simple case:

<Script language = "JavaScript">
<! --
// Insert your code here
// -->
</Script> however, in 2010, all browsers (or even cute IE 6) were able to interpret JavaScript code, so there was absolutely no need to wrap JavaScript code with annotations. Worse, if the code is wrapped in HTML comments and the-symbol is used, the browser may mistakenly assume that the HTML document has ended.

4. Framework
Unless your JavaScript code is very short or very simple, you should use the Framework to avoid repeated efforts on the code. In my opinion, jQuery is the best and has a great community, so it is worth trying.

Mango: in fact, YUI is also great, and the system is powerful, perfect, and secure. It also provides a Loader mechanism to improve Web application performance more efficiently.

5. Use the var keyword to declare a variable
You should use the var statement to declare the variable. Otherwise, the variable will exist in the global scope and use var to make the code readable and understandable.

For example:

Var name = "Jean ";
Var size = data. length; the variable scope issue also exists in the comments about the object reference mechanism in JavaScript.

6. Keep code separated
A few years ago, when a programmer wanted to add an event to an HTML element (for example, you wanted to verify the time information when the user entered it ), it uses special attributes to place JavaScript code in html, such as onblur, onchange, and onclick. For example:

<Input type = "text" name = "date" onchange = "validateDate ()"/> although still feasible, it is not concise. HTML should only contain the structure of the document, just as it is not good to use inline CSS, inline JavaScript is also not desirable.

Instead, what is the code below? JQuery is also easy to use:

$ (Document). ready (function (){
$ ('Input [name = date] '). bind ('change', validateDate );
}); 7. Include the script file at the bottom of the document
Not long ago, inserting a script file between the
To load the script after the content is ready, the JavaScript file should be included at the bottom of the document. As shown below:

<Script src = "myscript. js? "> </Script>
</Body>
</Html> 8. Use JSLint
JSLint is an application used for JavaScript source code check. If it finds some problems in JS, it will return the relevant problem description and approximate solution.
JSLint can effectively discover defects in the Code, or improve the Code style. This site is definitely worth collecting for JavaScript development.

9. Do not use document. write easily

The old document. write method has been in favor of using it for many years, but it is still a consistent method.

Document. write ("hello world"); Use the innerHTML attribute of DOM to insert text into the page.

Document. getElementById ('hello'). innerHTML = '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.