<香港科技大学html+css+js课堂笔记>week2--javaScript部分

1.格式:

<!DOCTYPE html>
<html>
<head>
. . . load JavaScript libraries here . . .
</head>
<body>
. . . your JavaScript code typically goes at the end of body . . .
</body>
</html>


2.html中两种引用js方式:

1>

<script>
function surprise() { 
    alert("Hello!");
}
</script>

2>

<script src="mycode.js"></script>
In mycode.js:
function surprise() {
    alert("Hello!");
}


3.

There are 3 JavaScript popups:
1>alert();
2>confirm();
3>prompt().

1>alert("Welcome!");

<香港科技大学html+css+js课堂笔记>week2--javaScript部分_第1张图片

2>confirm("want to go to Disneyland?");

源码:

<!doctype html>
<html>
    <head>
        <title>Example of confirm()</title>
        <script>
            if (confirm("Want to go to Disneyland?")) 
                document.location.href
                ="http://park.hongkongdisneyland.com";
        </script>
    </head>
</html>


3>prompt("what is your name?");

源码:

<!doctype html>
<html>
    <head>
        <title>Example of prompt()</title>
        <script>
            var user_name;
            user_name=prompt("What is your name?");
            document.write("Welcome to my page "
                + user_name + "!" );
        </script>
    </head>
</html>

<香港科技大学html+css+js课堂笔记>week2--javaScript部分_第2张图片


4.Variables:

1>data types:

number, String, Boolean, Other(Object)

2>number:

只有一种数字类型。

var big_number = 123e5;    //12300000
var small_number = 123e­5; //0.00123

3>String:

var name = "David"; 双引号
var title = 'Professor'; 单引号

var message = "It's alright";  双引号中可以直接使用单引号。

4>Boolean:

var condition1 = true;
var condition2 = false; 

5>TYPEOF:

You can use the typeof operator to check the type of a variable.

eg: typeof "bob" = String;

6>类型转换:



5.Function:

1>结构:

A function is a group of code:

function do_something() {   

    . . . code goes here . . .
}

Run the function like this:

do_something();


Eg:

<html>
    <head>
        <title>Example of a function</title>
        <script>
            function greet_the_user(){
                alert('Hello!');
                alert('We start soon...');
                prompt('Excited?!')
            }
        </script>
    </head>
    <body onload="greet_the_user()">
    </body>
</html>

2>recursive:

Eg:

<!doctype html>
<html><body>
  <script>
    alert("It's my " + build_great(5) +
          "grandmother!");
    function build_great( depth ) {
      if (depth > 0)
        return "great " + build_great( depth ­ 1 );
      else
        return "";
    }
</script>
</body></html>




你可能感兴趣的:(<香港科技大学html+css+js课堂笔记>week2--javaScript部分)