Pages

Tuesday, December 15, 2009

JAVA SCRIPT

JavaScript

  • JavaScript was designed to add interactivity to HTML pages
  • JavaScript is a scripting language
  • A scripting language is a lightweight programming language
  • 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

  • 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("name ") 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
  • 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

· JavaScript's official name is ECMAScript.

· ECMAScript is developed and maintained by the ECMA organization.

· ECMA-262 is the official JavaScript standard.

· The language was invented by Brendan Eich at Netscape (with Navigator 2.0), and has appeared in all Netscape and Microsoft browsers since 1996.

· The development of ECMA-262 started in 1996, and the first edition of was adopted by the ECMA General Assembly in June 1997.

· The standard was approved as an international ISO (ISO/IEC 16262) standard in 1998.

· The development of the standard is still in progress.

script type="text/javascript" src="xxx.js"

JavaScript is Case Sensitive

JavaScript Statements

A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do.

document.write("Hello Dolly");

Note: Using semicolons makes it possible to write multiple statements on one line.

JavaScript code (or just JavaScript) is a sequence of JavaScript statements.

Each statement is executed by the browser in the sequence they are written.

JavaScript statements can be grouped together in blocks.

Blocks start with a left curly bracket {, and ends with a right curly bracket }.

The purpose of a block is to make the sequence of statements execute together.

JavaScript Comments

Single line comments start with //.

Multi line comments start with /* and end with */.

Using Comments to Prevent Execution

JavaScript Variables

Rules for JavaScript variable names:

  • Variable names are case sensitive (y and Y are two different variables)
  • Variable names must begin with a letter or the underscore character

You can declare JavaScript variables with the var statement: var x; var carname;

var x=5; var carname="Volvo";

*If you redeclare a JavaScript variable, it will not lose its original value.

JavaScript Operators

= is used to assign values.

+ is used to add values.

JavaScript Arithmetic Operators

Operator

Description

Example

Result

+

Addition

x=y+2

x=7

-

Subtraction

x=y-2

x=3

*

Multiplication

x=y*2

x=10

/

Division

x=y/2

x=2.5

%

Modulus (division remainder)

x=y%2

x=1

++

Increment

x=++y

x=6

--

Decrement

x=--y

x=4

JavaScript Assignment Operators

Operator

Example

Same As

Result

=

x=y


x=5

+=

x+=y

x=x+y

x=15

-=

x-=y

x=x-y

x=5

*=

x*=y

x=x*y

x=50

/=

x/=y

x=x/y

x=2

%=

x%=y

x=x%y

x=0

Comparison Operators

Operator

Description

Example

=

is equal to

x= 8 is false

= =

is exactly equal to (value and type)

x= = 5 is true
x= = "5" is false

!=

is not equal

x!=8 is true


is greater than

x>8 is false

<

is less than

x<8>

>=

is greater than or equal to

x>=8 is false

<=

is less than or equal to

x<=8 is true

Logical Operators

Operator

Description

Example

&&

and

(x <> 1) is true

||

or

(x= =5 || y= =5) is false

!

not

!(x= =y) is true

Conditional Operator

Syntax

variablename=(condition)?value1:value2

Example

greeting=(visitor=="PRES")?"Dear President ":"Dear ";

No comments:

Post a Comment