w3r-javascript01: JavaScript Syntax

 

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.");

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值