22
JavaScript Basics

Session vii(java scriptbasics)

Embed Size (px)

Citation preview

JavaScript Basics

JavaScript is a programming language designed for Web

pages that describes data and procedures in terms of

objects, methods, and properties, rather than variables,

routines, and statements.

Unlike HTML, JavaScript is case sensitive. JavaScript is

plain text and when created in a text editor needs to be

saved as a text (ASCII) file.

Newer editions of full featured WYSIWYG HTML editors

support some Java Scripting, and often enable coding in

their page source windows.

If you begin doing JavaScript by cutting and pasting you

may often need to modify a few parts of the scripts, such

as names of objects. Many JavaScript's are available on

the WWW, and links to some popular JavaScript sites are

provided below.

A team of Netscape and Sun Microsystems developers

originally created JavaScript.

In 1998 the European Computer Manfacturers

Association (ECMA) announced the adoption of a

standard Internet scripting language based on JavaScript

1.1 that resolved the incompatibilities that existed in

scripting between various browser implementations.

Microsoft, Netscape,

and other browser companies agreed to follow the

specification. The standard is called ECMAScript,

though the common name, JavaScript, has persisted.

The most recent version of JavaScript is 1.3 (ECMA-

262).

Version 1.3 requires Netscape 4.06 or

newer and Microsoft Internet Explorer (IE) 4.0 or newer.

IE actually uses JScript, a modified version of JavaScript

that does not support all JavaScript commands.

I therefore recommend testing your pages, and using a

Netscape browser when viewing Web pages with

JavaScripting.

JavaScript can enhance the dynamics and interactive

features of Web pages by enabling calculations,

checking forms, writing interactive games, adding

special effects, customizing graphics selections, data

binding, and more. JavaScript is an interpreted

language that runs in the user's browser

. JavaScript code works on any computer platform

with a JavaScript capable browser, such as

Navigator 2.0 or later,

or Microsoft Internet Explorer 3.0 or later.

JavaScript capable browsers may allow disabling

JavaScript in their preferences. JavaScript is easier

to use than are programming languages.

With JavaScript, developers do not need to compile

a program or work with a developer's kit. Also, the

many JavaScripts available on the Internet can

easily be modified and adapted to your own pages.

JavaScript programming uses specialized terminology.

◦ Dot Syntax

◦ Objects.

◦ Properties

◦ Methods

◦ Events

◦ Functions

◦ Variables

◦ Expressions

◦ Operators

. Actions by page users trigger event handler

commands in the JavaScript. The program will

then perform whatever commands are assigned to

the event

. Common event handlers are listed in the following table.

Event Handler Action

onAbort The user stopped loading a page

onBlur The user moves from an object

onChange The user changed an object

onClickThe user mouse-clicked an object

onError An error occurred in the script

Event Handler Action

onMouseover The cursor has moved over the object

onMouseout The cursor has moved off the object

onSelect The user selected the object

onSubmit The user submitted a form

onUnload The user leaves the window

onFocus The user activates an object

onLoad The object has been loaded

JavaScript scripts can be embedded in an HTML page or

can reside in a separate page. 

JavaScript is often placed in the <head> section of the

HTML document, but can also be placed in the <body>. 

JavaScript object attributes can also be placed in HTML

element tags. You can use JavaScript in an HTML

document in the following ways:

As statements and functions within <script> and

</script> tags.

By specifying a file as the JavaScript source using the

<src> attribute of the <script> element.

By specifying a JavaScript expression as the value of an

HTML attribute and as event handlers within certain

other HTML tags.

The first method places the JavaScript between the

<script> and </script> tags. When specifying a script

only the tags <script> and </script> are essential.

It is recommendable to specify the script language as an

attribute of the script element. Browsers currently

assume JavaScript, but other programming languages

could become popular in the future.

The standard to open scripting is <script

language="JavaScript">.

The script language attribute can also specify the

version of JavaScript.

It is also recommendable to specify the MIME type of

the script, to denote to the browser that the script is

plain text. This is accomplished with the attribute

type="text/javascript".

The following example script redirects the browser to a page,

named javascript.html. The object is window, the variable property

is location, and the value equals the specified HTML file.

<script language="javascript1.2" type="text/javascript">

<!-- Begin hiding script

window.location="javascript.html"

// End hiding script-->

</script>

In the following examples I do not include the

<script>and </script> tags,

HTML tags or comment lines. These examples are

employed using the methods described above.

The first example is a method for displaying text in a

Web page.

The syntax for this method can also be writeIn, a

method that attaches a break to each line of text.

document.write("This is where you place the text

that will appear in the Web page.")

The write method also permits use of HTML tags

within the parenthesis, as seen below.

document.write("<b>Hello World.</b>")

The writeIn method should be used with

preformatted text.

var month = April;

var date = fifteenth;

document.write("Remember, your taxes are due on

" + month + date);