javascript: introduction

27
JavaScript: Introduction • Like C/C++ and Java, JavaScript is a case sensitive • It is not a Java • Interpreted Language • Embedded into HTML • Browser Dependent • Loosely Typed Language

Upload: baby

Post on 15-Jan-2016

45 views

Category:

Documents


0 download

DESCRIPTION

JavaScript: Introduction. Like C/C++ and Java, JavaScript is a case sensitive It is not a Java Interpreted Language Embedded into HTML Browser Dependent Loosely Typed Language. Basic JavaScript Code. . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: JavaScript: Introduction

JavaScript: Introduction

• Like C/C++ and Java, JavaScript is a case sensitive

• It is not a Java

• Interpreted Language

• Embedded into HTML

• Browser Dependent

• Loosely Typed Language

Page 2: JavaScript: Introduction

Basic JavaScript Code

<script language = "JavaScript"> <!-- your javascript code is here // --> </script>

Page 3: JavaScript: Introduction

Embedding JavaScript code into HTML document

<html> <head> <title>js01</title> </head> <body> <script language = "JavaScript"> <!-- alert("Hello, I'm from JavaScript") // --> </script> </body> </html>

Page 4: JavaScript: Introduction

Load the JavaScript code from external source file

External source file name: myscript.js

External source file content:alert("Hello, I'm from JavaScript")

HTML file content:<html> <head> <title>js01</title> </head> <body> <script src = "myscript.js"> </script> </body> </html>

Page 5: JavaScript: Introduction

Execute JavaScript code • Automaticaly when the page is loaded (in the example above)

• Write the Javascript code as a function <html> <head> <title>js02</title> <script language = "JavaScript"> <!-- function hello() {     alert("Hello, I'm from JavaScript") } // --> </script> </head> <body> </body> </html>

Page 6: JavaScript: Introduction

Execute JavaScript code

Execute JavaScript function using onLoad function:

<html> <head> <title>js02</title> . . . </head> <body onLoad="hello()"> </body> </html>

Page 7: JavaScript: Introduction

Execute JavaScript codeExecute JavaScript function by user action:

<html> <head> <title>js02</title> . . . </head> <body> <a href="javascript:hello()"> Clik me </a> (hypertext link) to execute Javascript function <form> <input  type="submit"  value="Click me (submit button) to execute Javascript function"  onClick="hello()"> </form> </body> </html>

Page 8: JavaScript: Introduction

Debugging JavaScript Code

• Just type "javascript:" in the URL field at your browser (Netscape)

• For IE click the status bar (bottom left ) to find out what the error was and what line it occurred on

Page 9: JavaScript: Introduction

JavaScript Keywords

if else while  for break

continue true false null return

int var in with this

function new      

Page 10: JavaScript: Introduction

Variable

var myVariable = "Pi"

yourVariable = 1.14159

alert(myVariable)

myVariable = 3.14159

newVariable = myVariable + yourVariable

alert(newVariable)

Page 11: JavaScript: Introduction

JavaScript Built-In Object Model

window

history

frame

location

Navigator

String

document

image

Array

Date

Math

applet

anchor

form

area

link

button

radio

text

reset

select

checkbox

submit

password

textarea

file

hidden

Page 12: JavaScript: Introduction

Accessing JavaScript Object Methods/Properties

•object_name.object_method

•object_name.object_property

Page 13: JavaScript: Introduction

Accessing JavaScript Object Methods/Properties: Example

<form name="my_form" method="POST">

<input type="text" name="ic" size=15>

</form>

To access the value of "ic", use the statement below:

document.my_form.ic.value

Page 14: JavaScript: Introduction

Using JavaScript Built-In Object Model

Write content into HTML document:

document.open()

document.write("<pre>\n")

document.write("Hello World\n")

document.writeln("How are You?")

document.write("World: I'm fine\n")

document.write("<pre>")document.close()

Page 15: JavaScript: Introduction

Open and write/set content into new browser

window:

Syntax:

windowVar = window.open("URL",

"windowName",

["windowAttributes"])

Using JavaScript Built-In Object Model

Page 16: JavaScript: Introduction

Open and write/set content into new browser

window:

Example:

newWin=window.open("", "myWindwow",

"width=400,height=300")

newWin.document.open()

newWin.document.write("Hello I\'m a new window")

newWin.document.close()

Using JavaScript Built-In Object Model

Page 17: JavaScript: Introduction

Get client browser information:

document.open()

document.writeln("<pre>")

document.writeln("<b>navigator.appCodeName = </b>" + navigator.appCodeName)

document.writeln("<b>navigator.appName = </b>" + navigator.appName)

document.writeln("<b>navigator.appVersion = </b>" + navigator.appVersion)

document.writeln("<b>navigator.mimeTypes.length = </b>" + navigator.mimeTypes.length)

document.writeln("<b>navigator.mimeTypes[0].type = </b>" + navigator.mimeTypes[0].type)

document.writeln("<b>navigator.mimeTypes[0].description = </b>" + navigator.mimeTypes[0].description)

document.writeln("<b>navigator.mimeTypes[0].suffixes = </b>" + navigator.mimeTypes[0].suffixes)

document.writeln("<b>navigator.userAgent = </b>" + navigator.userAgent)

document.writeln("</pre>")

document.close()

Using JavaScript Built-In Object Model

Page 18: JavaScript: Introduction

Get date information:

var today = new Date()

document.open()

document.write("Today is: " + today.getDate()

+ "/“

+ (today.getMonth() + 1) + "/"

+ (today.getYear() + 1900))

document.close()

Using JavaScript Built-In Object Model

Page 19: JavaScript: Introduction

Math properties/operation:

Using JavaScript Built-In Object Model

E LN10 LOG10E SQRT1_2

LN2 LOG2E PI SQRT2

abs cos min tan

acos eval pow toString

asin exp random valueOf

atan floor round  

atan2 log sin  

ceil max sqrt  

Page 20: JavaScript: Introduction

Define array variable using Array object:

var months = new Array()

months[0] = "Jan"

months[1] = "Feb"

. . .

. . .

months[11] = "Dis

Using JavaScript Built-In Object Model

Page 21: JavaScript: Introduction

Define array variable using Array object:

var days = new Array("Sunday", "Monday",

"Tuesday", "Wednesday",

"Thursday", "Friday",

"Saturday")

Using JavaScript Built-In Object Model

Page 22: JavaScript: Introduction

Define array variable using Array object:

var dayOfAugust = new Array(31)

for (var i = 0; i < 31; i++) {

dateOfAugust[i] = i + 1

}

Using JavaScript Built-In Object Model

Page 23: JavaScript: Introduction

String manipulation:

var str = "Hello World" len = str.length

len will get the value of 11

Using JavaScript Built-In Object Model

Page 24: JavaScript: Introduction

Using JavaScript Built-In Object Model

More string handling methods (str = “Hello World”):

Method Description Example

charAt(pos) Return the character at the specified index.

str.charAt(0) return a value of "H"

indexOf(searchText[, startPos])

Return the index of the first occurrence of searchText.

str.indexOf("or") return a value of 7

lastIndexOf(searchText[, startPos])

Return the index of the last occurrence of searchText.

str.lastIndexOf("l") return a value of 9

substring(startPos, endPos) Return the substring of the string starting at startPos and ending at endPos

str.substring(6, 8) return a value of "Wor"

Page 25: JavaScript: Introduction

Creating JavaScript new object

function object_name (property_1, . . ., property_n) { this.property_1 = property_1 . . . this.property_n = property_n

this.method_1 = method_name_1 . . . this.method_n = method_name_n}

function method_name_1() { . . .}

function method_name_n() { . . .}

Page 26: JavaScript: Introduction

Event Handler

Events:

• Generated by the system - when the browser loading a new page (onLoad event)

• Generated by the user - when the user click a button (onClick event)

Page 27: JavaScript: Introduction

Event HandlerTable of events and objects:  Event

Object onClick onSubmit OnChange onFocus onBlur onLoad onUnload onMouseOver onSelect

button X                

reset X                

submit X                

radio X                

checkbox X                

Link X             X  

Form   X              

Text     X X X       X

textarea     X X X       X

select     X X X        

window           X X