Java Script - Unit 1
Java Script - Unit 1
Unit - I
By:
Mrs. Manisha Sagade
Assistant Professor
Department of Electronics and
Telecommunication Engineering
PICT, Pune
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 learn the use of java script for controlling Windows and form handling
Course Outcomes
• CO1: Explain the basic features and fundamental concepts of Java Script and
Evaluate
an application using different data types and conditional & control statements
• CO2: Elaborate and predict the use of functions and objects in Java script.
• CO3: Identify and apply regular expressions used in java script for handling various
string operations.
Sections of a page
Content loading and
React to user actions, appearing and
changing dynamically
disappearing
What Can JavaScript Do?
</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"
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>
</body> </html>
</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
<head>attribute
<script>
function fun() {
alert("Welcome
to PICT"); }
</script>
</head>
<body>
<h3> This is an
example of
using onclick
attribute in
HTML. </h3>
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
<html>
<head>
Example
<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 event handler
of related to attributes eventssuch as
JavaScript
onmouseover, etc. which you canonclick,
bind
JavaScript statements to.
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 x = x+1
as
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 = 0;
x+y; return y = 10;
result; }
Variable
s
• 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.
• 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);
b
reak;
. . .
default
: statement(s);
}
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 between loops is often when the loop
• The difference
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;}
Functio
n using the function name
• We can then invoke a function
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
Functio
n
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.