The first glance at JavaScript

來源:互聯網
上載者:User

 

>>在哪裡放置 JavaScript

 JavaScript可以放置在Head或者Body之中,也可以從外部參考。

HTML內嵌JavaScript

<html><head><script type="text/javascript">document.write("<h1>This is a header</h1>"); </script></head><body><script type="text/javascript">document.write("<p>This is a body</p>");</script>
<script type="text/javascript">
//Write a "Good afternoon" greeting if
//the time is larger than 12
var d=new Date()
var time=d.getHours()
if (time>12) {
    document.write("<b>Good afternoon</b>");
}
</script></body></html>

 

外部參考JavaScript

把 .js 檔案放到網站目錄中通常存放指令碼的子目錄中,這樣更容易管理和維護。

<html><head><script type="text/javascript" src="externaljs.js">....</script></head><body></body></html>

 不過,從Performance的角度來看,無論是哪種方式,都放盡量發在Html Body的最後部分,免得Loading JavaScript會block影響整個html頁面的loading。

>> Values/Types

Booleans: true/false

Numbers: 1, 1.0

Strings: "abc", 'abc'

Functions:

    function twice(x) {        return 2 * x;    }    console.log(twice(4)); // 8

Objects:

var obj = {        propName1: 123,        propName2: "abc"};obj.propName1 = 456;obj["propName1"] = 456; // same as previous statement

Arrays:

var arr = [true, "abc", 123];console.log(arr[1]); // abcconsole.log(arr.length); // 3

undefined

null

>>條件控制

 if- else if - else:

<script type="text/javascript">var d = new Date();var time = d.getHours();if (time<10){    document.write("<b>Good morning</b>");}else if (time>10 && time<16){   document.write("<b>Good day</b>");}else{   document.write("<b>Hello World!</b>");}</script>

 

switch - case

<script type="text/javascript">//You will receive a different greeting based//on what day it is. Note that Sunday=0,//Monday=1, Tuesday=2, etc.var d=new Date();theDay=d.getDay();switch (theDay) {   case 5:     document.write("Finally Friday")     break   case 6:     document.write("Super Saturday")     break   case 0:     document.write("Sleepy Sunday")     break   default:     document.write("I'm looking forward to this weekend!")}</script>

 

 >>迴圈控制

 for loop:

<script type="text/javascript">var i=0;for (i=0;i<=10;i++){    document.write("The number is " + i);    document.write("<br />");}</script>

while:

<script type="text/javascript">var i=0while (i<=10){    document.write("The number is " + i);    document.write("<br />");    i=i+1;}</script>

 

do-while:

<script type="text/javascript">var i=0while (i<=10){    document.write("The number is " + i);    document.write("<br />");    i=i+1;}</script>

 

for ... in:

<script type="text/javascript">var x;var mycars = new Array();mycars[0] = "Saab";mycars[1] = "Volvo";mycars[2] = "BMW";for (x in mycars){    document.write(mycars[x] + "<br />");}</script>

 

break - continue:

<script type="text/javascript">var i=0;for (i=0;i<=10;i++){    if (i == 3){       break;    }    if(i == 4){        continue;    }    document.write("The number is " + i);    document.write("<br />");}</script>

 

>> Event

onload/onUnload:

    onload在匯入頁面的時候觸發,而onUnload則是在退出的時候觸發。

onFocus, onBlur 和 onChange:

    通常相互配合用來驗證表單。

onSubmit

    用於在提交表單之前驗證所有的表單域。

onMouseOver 和 onMouseOut

    常用來用來建立“動態”按鈕。

 

>> Error Handling

Exception(try/catch, throw):

<html>
<head>
<script type="text/javascript">
var txt=""
function message()
{
try
  {
    if(x>10)
        throw "Err1"
    else if(x<0)
        throw "Err2"
  }
catch(err)
  {
      txt="Unexcepted error found\n\n"
      txt+="Description: " + err.description + "\n\n"
      txt+="Choose OK to continue.\n\n"
      alert(txt)
  }
}
</script>
</head>

 

onerror:

<html><head><script type="text/javascript">onerror=handleErrvar txt=""function handleErr(msg,url,l){    txt="There was an error on this page.\n\n";    txt+="Error: " + msg + "\n";    txt+="URL: " + url + "\n";    txt+="Line: " + l + "\n\n";    txt+="Click OK to continue.\n\n";    alert(txt);    return true;}function message(){adddlert("Welcome guest!");}</script></head><body><input type="button" value="View message" onclick="message()" /></body></html>

 

>> More Materials:

JavaScript: The good part

Javascript 編程風格

JavaScript記憶體泄露

JavaScript W3C School

A quick overview of JavaScript

Major and minor JavaScript pitfalls and ECMAScript 6

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.