当前位置: 代码迷 >> 综合 >> w3r-javascript01: JavaScript Syntax
  详细解决方案

w3r-javascript01: JavaScript Syntax

热度:62   发布时间:2024-01-10 04:44:36.0

 

The script Tag

 

Syntax---src属性

This attribute specifies the location of an external script.

This attribute is useful for sharing functions among many different pages.

Note that external JavaScript files contain only JavaScript statements and files must have the extension .js.

 <script src = "common.js">JavaScript statements....... </script> 

 

 

Syntax---type属性

This attribute specifies the scripting language.

The scripting language is specified as a content type (e.g., "text/javascript" ).

The attribute is supported by all modern browser.

 <script type="text/javascript">JavaScript statements....... </script> 

 

 

 

Javascript in html document

 

Optionally, if your script is not required to be executed before the content of the body is displayed, and your script takes longer time to load objects, you can place your script at the end of the body element. 

 

为了让页面以最快的速度呈现给客户,可以将那些与页面初始化显示无关而且加载耗时的js脚本放到body标签的最后。

 

Script in the Head

 

  <!DOCTYPE html><head><meta charset="utf-8" /><title> Script in head section </title><script type = "text/javascript">JavaScript statements....... </script></head><body></body></html> 

 

Script in the Body

 

  <!DOCTYPE html><head><meta charset="utf-8" /><title> Script in the Body </title></head><body><script type = "text/javascript">JavaScript statements....... </script></body></html> 

 

Scripts in the Head and Body

 

  <!DOCTYPE html><head><meta charset="utf-8" /><title> Script in head and body section </title><script type = "text/javascript">JavaScript statements....... </script></head><body><script type = "text/javascript">JavaScript statements....... </script></body></html> 

 

Two Scripts in the Body

 

  <!DOCTYPE html><head><meta charset="utf-8" /><title> Two Scripts in the Body </title></head><body><script type = "text/javascript" scr="jsexample.js" ></script><script type = "text/javascript">JavaScript statements....... </script></body></html> 

 

 

Double or Single Quotes in JavaScript 

 

There is no preferred method, you can use either.

If you use one form of quote (either single or double) in the string, you may use other as the literal. 

 

双引号与单引号,在javascript都可以用在字符串上;

如果使用它们中的一个(单引号或双引号)在字符串上,你可以使用另一个来进行文本内容的修饰。

 

Case sensitivity

 

JavaScript is case sensitive.Therefore when we write any JavaScript word (for example "else") we must maintain proper case.

As JavaScript is case sensitive , a variable name "empcode" is not the same as "Empcode" or a function name "PayCalculation" is not the same as "paycalculation".
Always remember everything within <script type="text/javascript">and <script> tags (which are HTML tags) are case sensitive. 

 

JavaScript是大小写敏感的,区分大小写!

 

 

The Semi-Colon

 

In general JavaScript does not require a semi-colon at the end of a statement.

If you write two statements in a single line then a semi-colon is required at the end of the first statement.

However experienced programmers prefer to use a semi-colon at the end of each statement to make the code more readable and fix errors easily. 

 

一般而言,JavaScript不需要在每条语句后面加"分号";

但是,使用"分号"来分隔每条语句是一个好的做法,让代码更有可读性、维护性!

 

 

JavaScript Comments

Single line comment

单行注释

// This is a single line comment
alert("We are learning JavaScript single line comment");
//alert("Good Morning.");

 

Multiple line comments

多行注释

/* Multiple line comments start here
alert("Good Morning");
Multiple line comments end here*/
alert("We are learning JavaScript multiple
line comments.");

 

 

  相关解决方案