JavaScript Web-Technologies Notes
JavaScript Web-Technologies Notes
LEARNING MATERIAL
Features of JavaScript:
JavaScript is a lightweight, interpreted programming language means that
scripts execute without preliminary compilation.
It is an Object-based Scripting Language.
Designed for creating network-centric applications.
It is usually embedded directly into HTML Pages.
Java script code as written between <script>-----</script> tags
All Java script statements end with a semicolon
Java script ignores white space
Java script is case sensitive language
Script program can be saved as either .js or .html
Complementary to and integrated with Java.
Open and cross-platform.
Advantages of JavaScript:
Can put dynamic text into an HTML page
Used to Validate form input data
Javascript code can react to user events
Can be used to detect the visitor’s browser
Limitations of JavaScript:
Client-side JavaScript does not allow the reading or writing of files. This has
been kept for security reason.
JavaScript cannot be used for networking applications because there is no such
support available.
JavaScript doesn't have any multithreading or multiprocessor capabilities.
JAVA Vs JAVASCRIPT:
JAVA JAVASCRIPT
1. Object Oriented Programming
1. Object based Scripting Language
Language
2. Platform Independent 2. Browser Dependant
3. It is both compiled and
3. It is interpreted at runtime
interpreted
4. It is used to create server side
4. It is used to make the web pages
applications and standalone
more interactive
programming
5. Java is a strongly typed 5. JavaScript is not strongly
language typed(Loosely Typed)
6. Developed by sun Microsystems 6. Developed by Netscape
7. Java Programs can be 7. JavaScript must be placed inside an
standalone HTML document to function
Embed a JavaScript in an HTML document by using <script> and </script> html tags.
Syntax:
<script ...>
JavaScript code
</script>
<html>
<body>
<script language="javascript" type="text/javascript">
document.write ("Hello World!")
</script>
</body>
</html>
Comments in JavaScript:
JavaScript supports both C-style and C++-style comments. Thus:
Any text between a // and the end of a line is treated as a comment and is
ignored by JavaScript.
Any text between the characters /* and */ is treated as a comment. This may
span multiple lines.
2. VARIABLES:
• Like any programming language JavaScript has variables.
• Stores data items used in the script.
• Strict rules governing how you name your variables (Much like other languages):
Naming Conventions for Variables:
III YEAR-I SEMESTER A.Y.2018-19 CSE
Web Technologies 5
Creating Variables
• Before you use a variable in a JavaScript program, you must declare it.
Variables are declared with the var keyword as follows.
<script type="text/javascript">
var name;
var rollno;
</script>
<script type="text/javascript">
</script>
The scope of a variable is the region of your program in which it is defined and is
accessible. JavaScript variables have only two scopes.
Global Variables: A global variable has global scope which means it can be
defined and used anywhere in your JavaScript code.
Local Variables: A local variable will be visible only within a function where it is
defined. Function parameters are always local to that function.
Automatically Global:
•
If you assign a value to a variable that has not been declared, it will
automatically become a GLOBAL variable.
• This code example will declare a global variable price, even if the value is
assigned inside a function.
Example:
myFunction();
// code here can use price
function myFunction()
{
price = 250; //has Global scope
}
3. DATA TYPES:
JavaScript has only four types of data
Numeric
String
Boolean
Null
Numeric :
Integers such as 108 or 1120 or 2016
Floating point values like 23.42, -56.01 and 2E45.
No need to differentiate between.
In fact variables can change type within program.
String:
A String is a Collection of character.
All of the following are strings:
"Computer", "Digital" , "12345.432".
Put quotes around the value to a assign a variable:
name = "Uttam K.Roy";
Boolean:
Variables can hold the values true and false.
Used a lot in conditional tests (later).
Null:
Used when you don’t yet know something.
A null value means one that has not yet been decided.
It does not mean nil or zero and should NOT be used in that way.
4. FUNCTIONS:
• A function is a group of reusable code which can be called anywhere in your
program.
• This eliminates the need of writing the same code again and again.
• It helps programmers in writing modular codes. Functions allow a programmer
to divide a big program into a number of small and manageable functions.
• Like any other advanced programming language, JavaScript also supports all
the features necessary to write modular code using functions.
• We were using these functions again and again, but they had been written in
core JavaScript only once.
• JavaScript allows us to write our own functions as well.
Function Definition
Before we use a function, we need to define it.
The most common way to define a function in JavaScript is
By using keyword function, followed by a unique function name, a list of
parameters (that might be empty), and a statement block surrounded by
curly braces.
Syntax:
<script type="text/javascript">
function functionname(parameter-list)
{
statements
}
</script>
Example:
<script type="text/javascript">
function sayHello()
{
alert("Hello.. How are You");
}
</script>
Calling a Function:
To invoke a function somewhere later in the script, you would simply need to
write the name of that function as shown in the following code.
<html>
<head>
<script type="text/javascript">
function sayHello()
{
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type="button" onclick="sayHello()" value="Say Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>
5. OPERATORS:
JavaScript supports the following types of operators.
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical (or Relational) Operators
5. Conditional (or ternary) Operators
1. Arithmetic Operators:
JavaScript supports the following arithmetic operators:
Assume variable A holds 10 and variable B holds 20, then:
m = 20
Increments the value of a number by 1 n=++m
Prefix (Pre-increment) assigns 21 to n
++
Suffix (Post-increment) m = 20
n=m++
assigns 20 to n
m = 20
Decrements the value of a number by 1
n=--m assigns 19 to n
Prefix (Pre-Decrement)
-- m = 20
Suffix (Post-Decrement)
n=m++
assigns 20 to n
2. Assignment Operators:
3. Comparison Operators:
The if Statement
Syntax
if (condition)
{
block of code to be executed if the condition is true
}
}
else
{
block of code to be executed if the condition is false
}
if (condition1)
{
block of code to be executed if condition1 is true
}
else if (condition2)
{
block of code to be executed if the condition1 is false and condition2 is true
III YEAR-I SEMESTER A.Y.2018-19 CSE
Web Technologies 13
}
else
{
block of code to be executed if the condition1 is false and condition2 is false
}
Switch Statement:
Use the switch statement to select one of many blocks of code to be executed.
Syntax:
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}
while (condition)
{
code block to be executed
}
Example:
The do/while loop is a variant of the while loop. This loop will execute the code
block once, before checking if the condition is true, then it will repeat the loop as long
as the condition is true.
Syntax
do
{
code block to be executed
}while (condition);
The For Loop
{
code block to be executed
}
Statement 2 defines the condition for running the loop (the code block).
Statement 3 is executed each time after the loop (the code block) has been executed.
Example:
An Object is a thing.
There are pre defined objects and user defined objects in Javascript.
Document
Window
Browser/Navigator
III YEAR-I SEMESTER A.Y.2018-19 CSE
Web Technologies 16
Form
String
Math
Array
Date
HTML DOM
The way a document content is accessed and modified is called the Document
Object Model, or DOM.
The Objects are organized in a hierarchy. This hierarchical structure applies to the
organization of objects in a Web document.
Window object − Top of the hierarchy. It is the outmost element of the object
hierarchy.
Document object − Each HTML document that gets loaded into a window
becomes a document object. The document contains the contents of the page.
Form object − Everything enclosed in the <form>...</form> tags sets the form
object.
Form control elements − The form object contains all the elements defined for
that object such as text fields, buttons, radio buttons, and checkboxes.
The document object is the root node of the HTML document and the "owner" of all
other nodes:
(element nodes, text nodes, attribute nodes, and comment nodes).
The document object provides properties and methods to access all node objects, from
within JavaScript.
Tip: The document is a part of the Window object and can be accessed as
window.document.
Properties
alinkColor- The color of active links
Methods
getElementById(id)- Find an element by element id
WINDOW OBJECT:
document.getElementById("header");
Properties
defaultStatus - This is the default message that is loaded into the status bar when
the window loads.
opener The object that caused the window to open.
status - The status bar is the bar on the lower left side of the browser and is used
to display temporary messages
length - The number of frames that the window contains.
Methods
Properties
Properties
String The string object allows you to deal with strings of text.
Properties
Methods:
MATH OBJECT:
Properties:
E - Euler's constant
LN2 - Natural log of the value 2
LN10 - Natural log of the value 10
LOG2E - The base 2 log of euler's constant (e).
LOG10E - The base 10 log of euler's constant (e).
PI - 3.1428 - The number of radians in a 360 degree circle (there is no other
circle than a 360 degree circle) is 2 times PI.
SQRT1_2 - The square root of one half.
SQRT2 - The square root of 2.
Methods:
8. EVENT HANDLING:
JavaScript is an Event Driven System
Event:
An Event is “any change that the user makes to the state of the browser”
There are 2 types of events that can be used to trigger script:
1. Window Events
2. User Events
1. Window Events, which occurs when
A page loads or unloads
Focus is being moved to or away from a window or frame
After a period of time has elapsed
2. User Events, which occur when the user interacts with elements in the page using
mouse or a keyboard.
Event Handlers:
Event handlers are Javascript functions which you associate with an HTML
element as part of its definition in the HTML source code.
Syntax: <element attributes eventAttribute=”handler”>
Attribute Description
Onblur The input focus is moved from the object
The value of a field in a form has been changes by the user by
Onchange
entering or deleting data
</body>
</html>
Output:
2. <html>
<head>
<script language="javascript">
function fun()
{
alert("You Clicked on Button");
}
</script>
</head>
<body>
<input type="button" value="Click Me" onClick="fun()">
</body>
</html>
Output:
3. <html>
<head>
<script language="javascript">
function fun1()
{
n=parseInt(f1.t1.value);
document.writeln("Even Numbers from 1 to "+n+" are:");
for(i=1;i<=n;i++)
{
if(i%2==0)
document.write(i+" ");
}
}
</script>
</head>
<body>
<form name="f1" onSubmit="fun1()">
<label>ENTER A NUMBER:</label>
<input type="text" name="t1">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
HTML Vs DHTML
HTML DHTML
1. It is used to create static 1. Used to create dynamic web
web pages. pages.
2. Consists of simple HTML 2. Made up of HTML
tags. tags+CSS+javascript+DOM
3. It is a technique to make
3. It is a markup language. web pages dynamic through
client-side programming.
4. Do not allow to alter the text 4. DHTML allows you to alter
and graphics on the web the text and graphics of the
page unless web page gets web page without changing
changed. the entire web page.
5. Creation of HTML web pages 5. Creation of DHTML web
is simple. pages is complex.
6. Web pages are less 6. Web pages are more
interactive. interactive.
7. HTML sites will be slow 7. DHTML sites will be fast
upon client-side enough upon client-side
technologies. technologies.
Example:
<html>
<head>
<script language="javascript">
function img1()
{
i1.src="image2.jpg";
}
function img2()
{
III YEAR-I SEMESTER A.Y.2018-19 CSE
Web Technologies 29
i1.src="image1.jpg";
}
function fun1()
{
h11.innerText="You Clicked on Text";
}
</script>
</head>
<body>
<center>
<img src="image1.jpg" id="i1" width="320" height="290"
onmouseover="img1()" onmouseout="img2()">
</center>
<h1 id="h11" onclick="fun1()">Click on Text</h1>
</body>
</html>
Output:
UNIT-II
Assignment-Cum-Tutorial Questions
SECTION-A
Objective Questions
1. ______ tag is an extension to HTML that can enclose any number of JavaScript
statements. [ ]
A. <SCRIPT> B. <BODY> C. <HEAD> D. <TITLE
2. Which of the following best describes JavaScript? [ ]
A. a low-level programming language.
B. a scripting language precompiled in the browser.
C. a compiled scripting language.
D. an object-based scripting language.
SECTION-B
SUBJECTIVE QUESTIONS
1. Define JavaScript and Describe Primitive Data Types that JavaScript uses.
2. What is a function? Explain how parameters are passed to a function in JavaScript.
3. Define a variable. Explain different Scoping rules associated with variables in
JavaScript with example.
4. Illustrate various control statements available with JavaScript.
5. Write about the properties and methods of the following JavaScript Objects.
a) Document b) Form c) Window d) Browser e) Math
6. Describe an Event. Explain how events are handled in JavaScript with an Example.
7. Distinguish between HTML with DHTML.
8. Write a JavaScript that reads an integer and determines and displays whether it is an
odd or even number?
9. Create a JavaScript code to Print all numbers from 1 to 100 except multiples of 3.
10. Write a JavaScript to check whether given two numbers are equal or not. If not,
Display the Largest & Smallest among those two.
III YEAR-I SEMESTER A.Y.2018-19 CSE
Web Technologies 33
11. Write a JavaScript that reads an Integer and determine whether it is Prime Number
or not
12. Develop a JavaScript that reads an Integer and print its factorial.
13. Write a JavaScript which reads a number given and displays the output in words
(Eg:- Given 123, Output should be ONE TWO THREE)
14. Develope a JavaScript program to validate Login form consisting of username and
password (use regular expressions).
15. Create a JavaScript which has event handlers for the buttons “red”, “blue”, “green”,
“yellow” and “orange” which must produce messages stating the chosen favorite
color and applies it as a background color.
SECTION-C
QUESTIONS AT THE LEVEL OF GATE