1 what is javascript? javascript was designed to add interactivity to html pages javascript is a...

61
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight programming language A JavaScript consists of lines of executab le computer code A JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (mea ns that scripts execute without preliminar y compilation) Everyone can use JavaScript without purchasing a license

Upload: barbara-barlow

Post on 01-Apr-2015

234 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

1

What is JavaScript?

JavaScript was designed to add interactivity to HTML pages

JavaScript is a scripting language A scripting language is a lightweight programming

language A JavaScript consists of lines of executable computer co

de A JavaScript is usually embedded directly into HTML

pages JavaScript is an interpreted language (means that scripts

execute without preliminary compilation) Everyone can use JavaScript without purchasing a

license

Page 2: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

2

What can a JavaScript Do?

JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages

JavaScript can put dynamic text into an HTML page - A JavaScript statement like this: document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page

JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element

JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element

Page 3: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

3

What can a JavaScript Do?

JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing

JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser

JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer

Page 4: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

4

How to Put a JavaScript Into an HTML Page

<html>

<body>

<script type="text/javascript"> document.write("Hello World!")

</script>

</body>

</html>

Page 5: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

5

<html>

<body>

<script type="text/javascript">

...

</script>

</body>

</html>

Page 6: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

6

<script type="text/javascript">

<!–

document.write("Hello World!")

//-->

</script>

Page 7: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

7

Where to Put the JavaScript

Head section Body sectionExternal script

Page 8: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

8

Scripts in the head section

<html>

<head>

<script type="text/javascript">

....

</script>

</head>

Page 9: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

9

Scripts in the body section

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

Page 10: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

10

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

Page 11: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

11

Using an External JavaScript

<html> <head> <script src="xxx.js"></script> </head> <body> </body> </html>

Note: The external script cannot contain the <script> tag!

Page 12: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

12

Writing output to a page

document.write is a standard JavaScript command for writing output to a page.

document.write(“Hello, World!”)document is the objectwrite is the method

Page 13: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

13

JavaScript Popup Boxes

Alert Box Confirm BoxPrompt Box

Page 14: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

14

Alert box

An alert box is often used if you want to make sure information comes through to the user.

When an alert box pops up, the user will have to click "OK" to proceed.

Syntax:alert("sometext")

Page 15: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

15

Confirm Box

A confirm box is often used if you want the user to verify or accept something.

When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.

If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

Syntax: confirm("sometext")

Page 16: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

16

Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.

If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.

Syntax prompt("sometext","defaultvalue")

Page 17: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

17

JavaScript Variables

Variables are used to store data. A variable's value can change during the script

. You can refer to a variable by name to see its

value or to change its value.

Rules for variable names: Variable names are case sensitive They must begin with a letter or the underscor

e character

Page 18: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

18

Declaring and Using a Variable

It is declared by assigning it a value or using the var command var myVariable;

var myVariable=“Hello”;

myVariable=“Hello”;

Variables should be declared

immediately before using the variable

Page 19: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

19

Using the Prompt to assign Variables

The prompt allows the user to input a response

The response can be stored in a variable to be used elsewhere var visitorName;

visitorName=prompt(“What is your name?”, “Enter your name here.”);

Page 20: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

20

JavaScript Operators

Arithmetic Operators Assignment Operators Comparison Operators Logical Operators String Operator Conditional Operator

http://www.openjs.com/tutorials/basic_tutorial/operators.php

Page 21: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

21

Basic Math

Purchase 2 CDs and 3 dvdsvar cdPrice=12.00;var dvdPrice=15.00;var Totalcost;Totalcost= 2*cd + 3*vcd

Page 22: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

22

JavaScript Conditional Statements

Conditional statements in JavaScript are used to perform different actions based on different conditions.

Page 23: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

23

If Statement

if (condition)

{

code to be executed if condition is true

}

Page 24: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

24

If...else Statement

if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true }

Page 25: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

25

If...else if...else Statement

if (condition1) { code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if condition1 and condition2 are not true }

Page 26: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

26

JavaScript Switch Statement

switch(n) { case 1: execute code block 1 break case 2:execute code block 2 break default: code to be executed if n is different from case 1 and 2 }

Page 27: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

27

JavaScript Functions

A function is a reusable code-block that will be executed by an event, or when the function is called.

Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the <head> section.

Page 28: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

28

How to Define a Function

function functionname(var1,var2,...,varX)

{

some code

}

Page 29: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

29

Function with no parameters

function functionname()

{

some code

}

Page 30: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

30

The return Statement

The return statement is used to specify the value that is returned from the function.

Page 31: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

31

function prod(a,b)

{

x=a*b

return x

}

product=prod(2,3)

Page 32: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

32

JavaScript Loops

For loopWhile and Do while LoopFor ..in

Page 33: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

33

For Loop

for(var=startvalue;var<=endvalue;var=var+increment)

{

code to be executed

}

Page 34: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

34

For Loop

<html>

<body>

<script type="text/javascript">

var i=0

for (i=0;i<=10;i++)

{

document.write("The number is " + i) document.write("<br />")

}

</script>

</body>

</html>

Page 35: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

35

While and Do-While

while (var<=endvalue) { code to be executed }

do { code to be executed } while (var<=endvalue)

Page 36: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

36

While loop

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

document.write("<br />") i=i+1 } </script> </body> </html>

Page 37: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

37

Do -while

<html> <body> <script type="text/javascript"> var i=0 ;do { document.write("The number is " + i)

document.write("<br />") i=i+1 } while (i<0) </script> </body> </html>

Page 38: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

38

JavaScript For...In Statement

for (variable in object)

{

code to be executed

}

Page 39: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

39

Using for...in to loop through an array:

<html> <body><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></body> </html>

Page 40: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

40

JavaScript Events

Events are actions that can be detected by JavaScript.

Examples of events: A mouse click A web page or an image loading Mousing over a hot spot on the web page Selecting an input box in an HTML form Submitting an HTML form A keystroke

Page 41: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

41

ตั�วอย่�าง Event

onAbort เกิ�ดเมื่��อผู้��ใช้�ย่กิเลิ�กิกิารโหลิดเอกิสารหร�อภาพ onBlur เกิ�ดเมื่��ออ�อบเจ็�กิตั นั้�"นั้ถู�กิย่�าย่ focus ออกิไป onChange เมื่��อผู้��ใช้�เปลิ&�ย่นั้แปลิงค่�าในั้ฟอร มื่ร�บข้�อมื่�ลิ onClick เกิ�ดเมื่��ออ�อบเจ็�กิตั นั้�"นั้ถู�กิ click onError เกิ�ดเมื่��อกิารโหลิดเอกิสารหร�อภาพเกิ�ด ข้�อผู้�ดพลิาด onFocus เกิ�ดเมื่��ออ�อบเจ็�กิตั นั้�"นั้ถู�กิ focus onLoad เกิ�ดเมื่��อโหลิดเอกิสารเสร�จ็

Page 42: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

42

onMouseover เกิ�ดเมื่��ออ�อบเจ็�กิตั นั้�"นั้ถู�กิเลิ��อนั้เมื่าส ไปทั�บ

onMouseout เกิ�ดเมื่��ออ�อบเจ็�กิตั นั้�"นั้ถู�กิเลิ��อนั้เมื่าส ทั&�ทั�บออกิไป

onSelect เกิ�ดเมื่��อผู้��ใช้�เลิ�อกิข้�อค่วามื่ในั้ช้�องร�บข้�อค่วามื่

onSubmit เกิ�ดเมื่��อผู้��ใช้� submit แบบฟอร มื่ onUnload เกิ�ดเมื่��อผู้��ใช้�ออกิจ็ากิเวบเพจ็

Page 43: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

43

JavaScript Objects Introduction

JavaScript is an Object Oriented Programming (OOP) language.

An OOP language allows you to define your own objects and make your own variable types.

Page 44: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

44

Properties

Properties are the values associated with an object.

<script type="text/javascript">

var txt="Hello World!“;

document.write(txt.length);

</script>

Page 45: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

45

Methods

Methods are the actions that can be performed on objects.

<script type="text/javascript">var str="Hello world!"

document.write(str.toUpperCase) ()</script>

Page 46: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

46

JavaScript String Object

The String object is used to manipulate a stored piece of text.

Page 47: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

47

Length property

length property of the String object to find the length of a string:

var txt="Hello world!" document.write(txt.length)

Page 48: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

48

toUpperCase() method

the toUpperCase() method of the String object to convert a string to uppercase letters:

var txt="Hello world!" document.write(txt.toUpperCase())

Page 49: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

49

กิารด,าเนั้�นั้กิารกิ�บข้�อค่วามื่ CharAt(เลิข้ทั&�ตั,าแหนั้�ง) IndexOf(pattern) IndexOf(pattern,startIndex) lastIndexOf(pattern) lastIndexOf(pattern,startIndex) Spilt(separator) Substring(startIndex,endIndex) toLowerCase() toUpperCase()

Page 50: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

50

JavaScript Date Object

The Date object is used to work with dates and times. 

Defining Datesvar myDate=new Date()

Page 51: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

51

<html><body>

<script type="text/javascript">

document.write(Date())

</script>

</body></html>

Page 52: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

52

Date Object Method

getDate()getDay()getMonth()getFullyear()getYear()getHours()getMinutes()getSeconds()

Page 53: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

53

JavaScript Math Object

The Math object allows you to perform common mathematical tasks.

Page 54: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

54

Math Object Methods

max(x,y)min(x,y)pow(x,y)round(x)ceil(x)Etc...

Page 55: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

55

JavaScript Array Object

The Array object is used to store a set of values in a single variable name.

Defining Arrays

var myArray=new Array()

Page 56: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

56

var mycars=new Array() mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW"

var mycars=new Array(3) mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW"

Page 57: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

57

var mycars=new Array("Saab","Volvo","BMW") ว

Page 58: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

58

Accessing Arrays

You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.

document.write(mycars[0])

Page 59: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

59

More on Javascripts

http://www.openjs.com/tutorials/advanced_tutorial/

http://www.javascriptkit.com/jsref/image.shtml

http://www.javascriptkit.com/jsref/history.shtml

http://www.javascriptkit.com/jsref/navigator.shtml

Page 60: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

60

Navigator objectnavigator.appnamenavigator.appcodenamenavigator.appVersionnavigator.langaugenavigator.mimetype[]navigator.platformnavigator.plugin

Page 61: 1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight

61

สร�างเค่ร��องค่�ดเลิข้ด�งร�ป (ใช้�ร�ปทั&�

angsila.cs.buu.ac.th/~pan/321370/images)