Some Suggestions on using JavaScript

Source: Internet
Author: User

BKJIA exclusive Article] JavaScript is an extremely powerful and flexible scripting language, but for many people, "flexibility" also means easy mistakes. Here I will introduce the best practices of five JavaScript types, which can be applied in any JavaScript project and have high scalability.

BKJIA related articles: Seven JavaScript skills that should have been known

First, you must keep the code concise, clean, and high-quality comments. They are not unique to JavaScript, but many people do not pay attention to them. Compared with the comments of the Code itself, it is equally important to clarify the functions of each piece of code.

Second, save your JavaScript in an external file. The correct method to reference an external JS file is as follows:

 
 
  1. <script type="text/javascript" src="script.js"></script> 

Third, separate your JavaScript scripts from the presentation layer. An example of adding an onclick time from JavaScript is as follows:

 
 
  1. var div = document.getElementById('div');  
  2. div.onclick = new Function("processClick(this)"); 

Fourth, properly process and define variables. Many programmers do not properly define the scope of variables when programming, but simply use var.

Fifth, we should think carefully before using JavaScript, rather than putting JavaScript first. According to different users, many people may disagree with my ideas, but I still want to say: we should consider a small number of users, with an estimated 10%-of Web users not using JavaScript scripts) and reduce the use of JavaScript. In my opinion, JavaScript is like a helper tool, and we should not rely too much on it. Just like in the following example, those are the most important elements in the web page.

 
 
  1. <a href="javascript:processClick()">link</a> 
  2. <a href="#" onclick="javascript:processClick()">link</a> 

When a JavaScript script is disabled, the user clicks any of the links and nothing will happen. However, if the following code is used, even if JavaScript is disabled, they can still browse.

 
 
  1. <a href="link.html" onclick="processClick(); return false;">link</a> 

Title: 5 JavaScript Best Practices Author: Michael

Address: http://www.leigeber.com/2008/04/5-javascript-best-practices

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.