Javascript programming an introduction prepared by p d krolak and m s krolak
Download
1 / 101

JavaScript Programming an Introduction Prepared By P .D. Krolak and M.S. Krolak - PowerPoint PPT Presentation


  • 83 Views
  • Uploaded on

JavaScript Programming an Introduction Prepared By P .D. Krolak and M.S. Krolak. Based on material from JavaScript Unleashed (2nd Ed). Introduction to JavaScript. Basics for Non Programmers. JavaScript. JavaScript is a Programming Language, although not a complete one.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' JavaScript Programming an Introduction Prepared By P .D. Krolak and M.S. Krolak' - shadow


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Javascript programming an introduction prepared by p d krolak and m s krolak

JavaScript Programming an Introduction Prepared ByP .D. Krolak and M.S. Krolak

Based on material from

JavaScript Unleashed (2nd Ed)


Introduction to javascript

Introduction to JavaScript

Basics for Non Programmers


Javascript
JavaScript

  • JavaScript is a Programming Language, although not a complete one.

  • JavaScript was developed by Netscape and is becoming an international standard, ECMA-262.

  • JavaScript is sent with the HTML document and is interpreted at the time it is loaded on the browser

  • JavaScript adds interaction



Embedding js in html1
Embedding JS in HTML

  • The <script> </script> tag container

  • Accommodating the Non JS supporting browsers by using a comment to surround the content of the script within the container.

  • Viewing the JS code

  • Executing the scripts

  • loading a page

  • HTML enhancements


The script tag
The <script> tag

  • <script language=“JavaScript1.x”> where x can be version of JavaScript used, i.e 0,1,2,3. If the browser does not support the version the tag and its content is ignored.

  • language can also be set to vb script, php, etc.

  • <Script > </Script> form the container

  • The script may be placed anywhere in the document but must exit before needed.

  • Most scripts are placed in the head and are thus loaded before they are needed for the display & interaction.

  • Scripts that contain document.write() must be in the body.


The script tag continued
The <script> tag continued

  • JavaScript libraries may be stored external to the HTML document.

  • <script Language=javascript1.2 src=“Library’s_URL”>

  • The Library file extension must be “.js”

  • The External file does not have the <script></script> tags


Viewing the js code a simple hello world
Viewing the JS codeA Simple Hello World

<Html>

<Head> <Title>JS Sample</Title>

</Head>

<Body>

<script Language=“javascript1.2”>

document.write(“Hello World”);

</Script>

</Body>

</Html>

See the above web page


Viewing the js code a confirm hello world
Viewing the JS codeA Confirm Hello World

<Html>

<Head> <Title>JS Sample</Title>

</Head>

<Body>

<script Language=“javascript1.2”>

confirm(“Hello World”);

</Script>

</Body>

</Html>

See the above web page


Viewing the js code an alert window hello world
Viewing the JS codeAn Alert window -- Hello World

<Html>

<Head> <Title>JS Sample</Title>

</Head>

<Body>

<script Language=“javascript1.2”>

alert(“Hello World”);

</Script>

</Body>

</Html>

See the above web page


Accommodating the non js supporting browsers
Accommodating the Non JS supporting browsers

  • The contents of the script container are commented so non JS browsers will ignore the contents.

  • <Script >

    <! -- // the comment is used by the non-JS

    Your JavaScript code here

    -->

    </Script>


Executing the scripts
Executing the scripts

  • The scripts are not executed in any necessary order.

  • They are executed as the flow of events dictates. Events are generally created by an action of the user interacting with the page elements. An example, a mouse over a link.


Loading a web page
Loading a web page

There is no difference in loading a page with a script or without one.






Javascript data types
JavaScript Data Types

  • JavaScript does not have a rich set of data types like most widely used programming languages.

  • JavaScript is also not strongly typed.










Js operators
JS Operators

  • Assignment Operators

  • Arithmetic Operators

  • Comparison Operators

  • String Operators

  • Conditional Operators

  • Boolean Operators

  • The type of operators


Assignment
Assignment

Assignment operator assigns a value to a variable.





Arithmetic
Arithmetic

Arithmetic operators take numeric variables and return a single numeric value





Comparison
Comparison

A comparison operator logically compares its operands and returns a logical True or False.




String operators
String Operators

String operators are the same as comparison, using a sort sequence. This is called a lexicographic or dictionary sort

String a> b, or a>=b etc.

String has two forms of concatenation, i.e.

(the string ‘b’ is added to the end of string ‘a’)

  • a+b

  • a +=b



Conditional
Conditional

Assign a value if the condition is true or the alternative value if not.

(condition) ? True Value : False Value


Boolean
Boolean

A Boolean operator takes two operands and returns a truth value.



Typeof operand
Typeof Operand

Typeof operand returns the data type of the operand.


Js operators continued
JS Operators (continued)

  • Function operators

  • Data structure operators

  • Bit wise operators

  • Operator precedence




Statements
Statements

  • Statements define the flow of the script.

  • JS statements require a “;” at the end if there are more than one on a line.

  • A block of statement that are executed in order is enclosed by curly brackets {}.

  • Flow is normally linear but may be altered by conditional, looping, or similar statements.


Comments
Comments

  • Comments are not really part of the program statement but are provided for the programmer and others as notes and reminders of what is happening in the JS.

    Statement; //single line comment

    /* this the way to

    create multi line comments */


Js control structures and looping
JS Control Structures and Looping

  • Conditional statements

  • Looping statements

  • Labeled

  • with

  • switch


Conditional statements
Conditional statements

If …. else

If (condition)

{Block of Statements;}

else

{Block of Statements;}


Looping statements
Looping statements

For

The traditional for loop -- loops until the test condition is false. The initial statement is executed once. The test is applied and if true, the Block of Statements are executed. The counter is incremented and test applied. Repeat looping.

for (initial_statement; test_condition; increment)

{Block of Statements;}


Looping statements1
Looping statements

For … in

On each iteration get one property of the object. If the object has no properties the loop is not executed.

for (property in object)

{Block of Statements;}


Looping statements2
Looping statements

do …. while

Repeat the block statement until the condition is false.

(Note it goes thru loop at least once)

do

{ Block of Statements; }

while (condition)


Looping statements3
Looping statements

While

While executes as long as condition is true. Will not execute the first time if false.

While (condition)

{ Block of Statements;}


Break continue statements
Break & Continue Statements

  • Break – Drop out of loop and go to statement following loop.

  • Continue – Drop out of the loop block of statements and go to the loop control. Looping continues until control is false.


Labeled
Labeled

  • Label statement – allows the break and continue statement to transfer to this label.

    Label: statement;


with

  • Establishes an object as the point of reference.

    with (object)

    {Block of Statement;}


Switch
switch

Switch is the JavaScript case statement.

Switch (expression){

case label-1:

{Block of Statements;}

break

….

case label-n:

{Block of Statements;}

break

default:

{Block of Statements;}

}



Js as object orientated language
JS as Object Orientated Language

  • Object and Dot Notation

  • Properties

  • Methods

  • Events


Object and dot notation
Object and Dot Notation

The object is described from the container that holds the container that holds .. the container. Read from left to right and the dot as contains.

Example the form object from the diagram

Window.document.form


Object dot notation
Object Dot Notation

  • A property is:

    Object.property where dot reads is_a

  • A method is:

    Object.method() where dot reads is_a


Object dot notation1
Object Dot Notation

  • Since a window contains everything, it is called self.

  • In the Dot notation we can drop the window because it is contained by nothing and contains everything.


Javascript objects1
JavaScript Objects

  • JS is not true Object Oriented Programming (OOP) but object like

  • What are the JS’s objects

  • What is the JS object Hierarchy

  • Built-In Language Objects


Js is not true oop but object like
JS is not true OOP but object like.

  • JS objects do have properties and methods like and object orientated language.

  • JS objects do not support inheritance.

  • JS object model is a container model not a class model.

  • Container objects contain other containers.


What are the js s objects
What are the JS’s objects

  • JavaScript objects fall into classes

    • Navigator Objects that mostly correspond to HTML tags

    • Built-in Language Objects




Javascript properties
JavaScript Properties

  • Properties in JS resemble data attributes of an object

  • Properties explain the characteristics and identity of the given object

  • Properties can represent the state of the object

  • Properties could represent the role that the object plays at a given time


Javascript events event handlers
JavaScript Events & Event Handlers

  • A JavaScript event is controlled by browser based on action normally initiated by the viewer.

  • Each tag has an associated set of event handlers that are triggered by a corresponding event. When the event handler is triggered, a JavaScript is preformed.


Javascript methods
JavaScript Methods

Methods are functions that provide services for the object

  • Set the value of a property

  • Get the value of a property

  • Iterate on the object’s properties

  • Constructor for the object.


Js window object
JS Window Object

  • The Window Object contains all the objects.


Js navigator object
JS Navigator object

  • Contains information about the browser.

  • Properties can not be set by JavaScript.


Js history object
JS History Object

  • The History object records the documents that were displayed in order of their presentation – most recent descending to oldest.




Js history example
JS History Example

Create a back to last page link

<a HREF=“JavaScript:history.back();”>

Go to Last Page </a>

In the above link tag the href is:

  • set to a JavaScript

  • Returns the link found in the history object using the back method with no argument, i.e. the default is the URL of the previous link.

  • When clicked the link returns to the previous link


Js document object
JS Document Object

  • Contains all the objects that are contained in the document. The objects that are contained correspond to the HTML tags found on the web page.


Js location object
JS Location Object

  • Location Object contains the information about the source of the document, i.e. the URL, and related information.


Js frame object
JS Frame Object

  • Contains information about the frames in the widow.


Built in language objects
Built-In Language Objects

  • Built-in objects do not appear in the document but in the code.

  • Built-in objects include:

    • Array

    • Date

    • Math

    • Regular Expression

    • String












Js object constructor
JS Object Constructor

A method is a constructor if it has the same name as the object an provides a template for creating an instance of the object.

Function object(parm1, … ){

This.property1 = parm1 ;

..

This.method1= function1

..

}




Debugging javascript1
Debugging JavaScript

  • First sign of trouble will appear as error message in the status window (lower left bottom of window).

    When a error does appear then type:

    Javascript: in the location window (where the document URL goes). Note the last character “:” is required. A series of error messages that will attempt to locate the error will appear in the dialog window.


When creating javascript turn javascript console automatically
When creating JavaScript turn JavaScript Console automatically

  • Can not be done through preference option But it can done.

  • Go to the pref.js in the Netscape directory.

  • Open in a text editor.

  • Last line add:

    user_pref(“javascript.console.open_on_error”,

    true);

  • Save file and Restart Netscape.

    Console should pop up when a JavaScript error occurs.


Netscape js debugger
Netscape JS Debugger automatically

  • Download the JS debugger from their web site:

    http://developer.netscape.com/software/jsdebug.html



Javascript references

JavaScript References automatically


Javascript references1
JavaScript References automatically

  • Richard Wagner, et al. JavaScript Unleashed (Second Ed.), Sams.net (1997)

  • Michael Moncur, SAMS Teach Yourself JavaScript in 24 Hours (Second Ed.), Sams Pubs. (2000).

  • Ellie Quigley, JavaScript by Example, Prentice Hall, (2001).


ad