FOP- Java Script_Unit 1 (1)
FOP- Java Script_Unit 1 (1)
Java Script
By:
Dr. Khyati R Nirmal
Associate Professor
Department of Computer Engineering
SNJB’s LS KBJ CoE, Chandwad
Java Script
• Teaching Scheme: Theory: 03 hrs. / week
• Credit: 03
• Examination Scheme:
• In-Sem (Theory): 30 Marks
• End Sem (Theory): 70 Marks)
• Prerequisite Courses:
• Fundamentals of Java Programming,
• Advanced Java Programming
Course Objectives
• To learn the syntax and semantics of Java script.
• To learn how to use regular expressions in java script for handling various string
operations.
• To understand the concept of object models and event handling in java script
programs.
• To learn the use of java script for controlling Windows and form handling
Course Outcomes
• CO1: Use basic features of java script
• CO2: Use relevant data types for developing application in java script
• CO3: Use the function and objects as self-contained, with data passing in and out
through well-defined interfaces in development of small systems
• CO4: Apply the regular expression for Text matching and manipulation
• CO5: Explore use of the various aspects of JavaScript object models that are
fundamental to the proper use of the language
• CO6: Develop the application using windows controlling and form handling
Unit 1: Introduction to Java Scripts
• Introduction – First Look at JavaScript, Adding JavaScript to
XHTML Documents- The <script> Element, Using the <script>
Element, Event Handlers, Linked Scripts
• History and Use of JavaScript
• JavaScript Core Features- Overview-Basic Definitions, Language
Characteristics, Variables, Basic Data Types, Composite Types,
• Flow Control Statements, Loops, Functions, Input and Output in
JavaScript, Regular Expressions
Sections of a page
Content loading and
React to user actions, appearing and
changing dynamically
disappearing
What Can JavaScript Do?
Script in Script in
Script in
Script in External file and then include
link in HTML file head section
<head> and <body> section
First Look at JavaScript
<html>
<head>
<title>JavaScript Hello World</title>
</head>
<body>
<h1 align="center">First JavaScript</h1>
<script type="text/javascript">
document.write("Hello World from JavaScript!");
</script>
</body> Write a string to the browser.
</html>
First Look at JavaScript
• Using the <script> element allows the browser to
differentiate between what is JavaScript and what is
(X)HTML markup or regular text
<script type="text/javascript">
<strong>
document.write("Hello World from JavaScript!");
</strong>
</script>
Markup Language Vs Scripting Language
<script type="text/javascript">
<strong>
document.write("Hello World from JavaScript!");
</strong>
</script>
<script type="text/javascript">
document.write("<strong>Hello World</strong>")
</script>
Adding JavaScript to XHTML Documents
• <script> . . . </script>
• By default <script> tends to interpret contents as
JavaScript
• You can also include the following attributes to explicitly
specify javascript
• Inclusion of language attribute
<script type="text/javascript"
language="javascript">
25
1. Within <script> element
Within <body> tag Within <head> tag
<html> <html>
<head> <head>
</head> <script>
<body> document.write("hello")
<script> </script>
document.write("hello") </head>
</script>
</html>
</body>
</html>
2. As a linked file via the src attribute
myScript.js
function myFunction() {
document.getElementById("demo").innerHTML = "Paragr
aph changed.";
}
2. As a linked file via the src attribute
demoextr.htm
<html>
<body>
<h2>Demo External JavaScript</h2>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
<p>This example links to "myScript.js".</p>
<p>(myFunction is stored in "myScript.js")</p>
<script src="myScript.js"></script>
</body>
</html>
3. XHTML event handler attribute
<head>
<script>
function fun() {
alert("Welcome to SNJB"); }
</script>
</head>
<body>
<h3> This is an example of using onclick attribute in HTML. </h3>
<p> Click the following button to see the effect. </p>
<button onclick = "fun()">Click me</button>
</body>
</html>
4. Via the pseudo-URL
<script>
document.write("<html><body>Hello World!</body></html>");
</script>
Hiding JavaScript from Outdated Browsers
• Older browsers don't understand the <script> tag.
31
Script Hiding and <noscript>
• Script Hiding using HTML and JavaScript comments
<script language="javascript"
type="text/javascript">
<!--
put your JavaScript here
//-->
</script>
• Avoids printing script onscreen in non-script aware browsers
32
Script Hiding and <noscript>
• <noscript> Element
• Useful to provide alternative rendering in browsers that have script off or don’t support
script
<noscript>
<h1>Your browser does not support JavaScript or
it is currently disabled.</h1>
</noscript>
33
<noscript> Exclusion Example
<html>
<head>
<title>Needs JavaScript</title>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
document.write("Congratulations! If you see this you have JavaScript.");
//-->
</script>
<noscript>
<b>JavaScript required</b><br>
</noscript>
</body>
</html>
34
Event Handlers
• HTML defines a set of event handler attributes
related to JavaScript events such as onclick,
onmouseover, etc. which you can bind JavaScript
statements to.
mouseover onmouseover When the cursor of the mouse comes over the
element
mouseout onmouseout When the cursor of the mouse leaves an element
unload onunload When the visitor leaves the current webpage, the
browser unloads it
resize onresize When the visitor resizes the window of the browser
Event Handlers-Demo
Navigation systems
• Whitespace
• Whitespace is generally ignored in JavaScript
statements and between JavaScript statements,
• e.g.
• x = x + 1 same as x = x + 1
• s = typeof x; is the same as s=typeof x;
• but it is not the same as s=typeofx; or s= type of x;
Language Characteristics
• Statements
• A script is made up of individual statements
• JavaScript statements are terminated by ENTERs or semi-colons (;)
• So x = x+1; same as x = x+1
alert(x); alert(x)
• Prefer to use semi-colons because if you reduce ENTERs you run into
problems
x=x+1 alert(x) throws an error while x=x+1;alert(x); does
not.
Language Characteristics
• To group together statements we can create a block using
curly braces { }. In some sense this creates one large
statement
• Blocks are used with functions as well as larger decision
structures like if statements
function add(x,y) { if (x > 10) {
var result = x+y; x = 0;
return result; y = 10;
} }
Variables
• Variables store data in a program
• The name of a variable should be uniquely well formed
identifier starting with a letter and followed by letters or
digits
• Variable names should not contain special characters or
white space
• Variable names should be well considered,
e.g. "sum" is better than "x"
Variables – Variable Scope
• determines the level of accessibility to this variable.
• Variables declared outside of any function are said to have global
scope, thus accessible everywhere in your JavaScript code
• Defining arrays
var myArray = [1, 5, 1968, 3];
var myArray2 = ["Thomas", true, 3, -47];
var myArray3 = new Array();
var myArray4 = new Array(10);
var myArray5 = new Array("Tom", "Roy", "Al");
Composite Types- Array
• Access arrays by index value
var myArray = new Array(4)
myArray[3] = "Hello";
• Arrays in JavaScript are 0-based, e.g.
• var myArray1 = ["Thomas", true, 3, -47];
• myArray1[0] is "Thomas", myArray1[1] is true and so on
• Given new Array(4) you have an array with an index running from
0 to 3
• To access an array length you can use arrayName.length
(e.g. alert(myArray2.length); )
Composite Types- Object
• Objects can hold any type of data and are the primary
mechanism by which useful tasks are carried out. The
browser provides a large number of objects for you to use
• For example, you can interact with the user through the
Window object or modify the contents of a Web page with
the Document object.
Composite Types- Object
• Data contained in an object are said to be properties of the
object. Properties are accessed with the “dot” operator,
which is simply a period followed by the property name.
• The syntax is
• objectname.propertyname
switch (condition) {
case (value1): statement(s);
break;
case (value2): statement(s);
break;
. . .
default: statement(s);
}
• The switch statement is not supported by very old JavaScript aware browsers (pre-
JavaScript 1.2), but today this is not such an important issue
Flow Control Statements :Loop
• JavaScript supports three types of loops: while, do/while, and for
• Syntax of while:
while(condition)
statement(s)
• Example:
var x=0;
while (x < 10){
document.write(x);
document.write("<br>");
x = x + 1;
}
document.write("Done");
Flow Control Statements : do Loop
• The difference between loops is often when the loop
condition check is made, for example
var x=0;
do {
document.write(x);
x = x + 1;
} while (x < 10);
• For example
function add(x, y){
var sum = x + y;
return sum;}
Function
• We can then invoke a function using the function name
with ( )’s
var result = add(2, 3);
• We can also pass variable values as well as literals
var a=3, b=5;
var result;
result = add(a, b);
• Variables are passed to function by value so you must use
return to send things back.
• You can return a value or not from a function and you can
have as many return statements as you like
Function
Input and Output in JavaScript
• JavaScript executes in a host environment like a Web
browser, its
• I/O facilities might be different from what you would
expect.
• For obvious security reasons, plain client-side JavaScript
is not usually allowed to read or write files in the local
file system.
Input and Output in JavaScript
• Interacting with the user is typically achieved through the
• Window object, several methods of which are described
here.
• One of the most common I/O methods in JavaScript is
using the alert() method of Window,
• which displays its argument message in a dialog box that
includes an OK button.
• For example,
alert("This is an important message!");
Input and Output in JavaScript
• Other forms of dialog with the user include
the confirm() method, which displays its
argument message in a dialog box with both
OK and Cancel buttons.
• With the script
confirm("Learn JavaScript?");
Input and Output in JavaScript
• the prompt() method to collect some data from
the user.
• A prompt displays its argument message in a
dialog box and allows the user to
• enter data into a text field, as illustrated by
this example:
• var answer = prompt("What is your favorite
color?","");
Regular Expression
• The last major functional feature of JavaScript is the regular expression.
• A regular expression as defined by the RegExp constructor is used to
carry out pattern matching.
• var country = new RegExp("England");
• This could have been defined as well using a direct assignment:
• var country = /England/;
• Once a regular expression is defined, we can use it to pattern-match and
potentially change strings.
• The following simple example matches a piece of the string in the
variable .
Regular Expression
• geographicLocation and substitutes it for another string.