Best practices for modern Javascript development
来源:互联网 发布:学生空间七天网络下载 编辑:程序博客网 时间:2024/05/21 19:52
Best practices for modern Javascript development
Javascript is definitely a very popular language on the web, and every web developer has to deal with it someday. For this article I have compiled a non-exhaustive list of good practices that will help you to become a better javascript developer.
Use the correct <script> tag
When you have to use some Javascript in an html file, you should always use the following <script>
tag:
<
script
type
=
"text/javascript"
>
... some JS code
</
script
>
But instead, how many times have you seen this when looking at the source code?
<
script
type
=
"text/javascript"
language
=
"javascript"
>
... some JS code
</
script
>
In HTML, the language
attribute is deprecated due to its redundancy with the type
attribute. You should never use it anymore.
Keep your code in an external file
Using an external .js
file for your Javascript code is a lot cleaner than writing it in your html document, and it also allows the browser to cache the file, which will result in a faster website.
Put your Javascript code in a .js
file, then use the <script>
tag in your html document to import the file:
<
script
type
=
'text/javascript'
src
=
'http://www.catswhocode.com/myscript.js'
></
script
>
Don’t wrap code in HTML comments
In the 90′s some very old browsers weren’t able to interpret Javascript. In order to prevent unwanted results on those browsers, it was considered good practice in 1994-1997 to wrap Javascript code within html comments, so browsers with no Javascript support will simply ignore it.
Here is an example of some code wrapped within html comments:
<
script
language
=
"JavaScript"
>
<!--
...some code
//-->
</
script
>
However, in 2010, all browsers (Even IE6, that means a lot) can interpret Javascript, so there’s absolutely no need to wrap code within comments anymore. Even worse, if code is wrapped within comments and use the decrement symbol --
, you’ll expect some weird problems due to the fact the browser may think it’s the end of the html comment.
Use a framework
Unless your Javascript code is really short and easy, you should always avoid reinventing the wheel by using a framework of your choice. In my opinion, jQuery is the best and has an awesome community, so you should give it a try if you haven’t already.
Always declare your variables using var
You should introduce any variable you create with the var
statement, otherwise it gets to the global scope. Also, using var
makes your code more readable and self-explanatory.
Example of variable created using the var
statement:
var
name =
"Jean"
;
var
size = data.length;
Keep your code unobtrusive
Some years ago, when a programmer wanted to add an event to an html element (for example, if you want to validate a date when the user typed something) he simply put Javascript code in the html, using a special attribute such as onblur
, onchange
, onclick
, etc.
Example:
<
input
type
=
"text"
name
=
"date"
onchange
=
"validateDate()"
/>
This works great, but it is a bit dirty. HTML should only contain the document description. Just like it’s bad practice to use inline CSS styles, it’s bad practice to use inline Javascript.
Instead, what about using some unobtrusive Javascript? Using jQuery, it is pretty easy to do, as you can see in the following example:
$(document).ready(
function
(){
$(
'input[name=date]'
).bind(
'change'
, validateDate);
});
Include your scripts at the bottom of your HTML files
Not so long ago, it was generally considered good practice to insert your Javascript files within the <head>
and </head>
tags of your html document.
But browsers read html files from top to bottom, and load external files dynamically. Which mean that inserting scripts within the <head>
and </head>
tags will make your Javascript load before some of the page content.
In order to always load scripts after the content, Javascript files should always been included at the bottom of your html files, as shown below:
<
script
src
=
"myscript.js?"
type
=
"text/javascript"
></
script
>
</
body
>
</
html
>
Use JSLint
JSLint is a web-app which takes a JavaScript source and scans it. If it finds a problem, it returns a message describing the problem and an approximate solution.
JSLint is great to find bugs in your code, and also things that may be written in a better way. This site is definitely my favorite coding buddy when developing some Javascript.
Don’t use document.write
The good old document.write
method has been deprecated for years, however it is still very common to see it while browsing code.
document.write(
"hello world"
);
Instead of using this deprecated method, you should use the DOM and the innerHTML
function to insert text on a page:
document.getElementById(
'hello'
).innerHTML(
'hello world'
);
- Best practices for modern Javascript development
- Best Practices and Requirements for OpenSolaris development
- Preparing Yourself for Modern JavaScript Development
- Some Best Practices for Silverlight Application Development (XAML)
- 24 JavaScript Best Practices for Beginners
- 24 JavaScript Best Practices for Beginners
- 24 JavaScript Best Practices for Beginners
- 24 JavaScript Best Practices for Beginners
- 24 JavaScript Best Practices for Beginners
- 24 JavaScript Best Practices for Beginners
- Best practices in Android development
- Best practices in Android development
- Best practices in Android development
- Best practices in Android development
- Best Practices for WOW64
- Best Practices for Performance
- Best Practices for Performance
- Javascript Best Practices
- WinForm 控件 简单设置快捷键
- Linux 静态库与动态库搜索路径设置
- linux输入子系统(3)
- 豪盛手台操作方法
- QT: sort dialog
- Best practices for modern Javascript development
- linux输入子系统(4)
- 百度空间不适合开发BLOG,所以准备转战CSDN,专业!
- 常用数据库 取前几条数据 sql写法
- VS与Win7共舞:系统服务的Session 0隔离
- servlet的session管理
- 软件工程师发展之路之我见(一)
- TOP 字句的运用
- linux输入子系统(5)