02.04. JavaScript-2
02.04. JavaScript-2
Javascript
Prepared By:
Darsha Chauhan
● Introduction
● Task Performed by Client side Scripts
● Pros & Cons of Client side Scripts
● Client side Scripts V/S Server side Scripts
● Variables
● Functions
Statements {
case lbl1:
// code to execute
break;
case lbl2:
// code to execute
break;
}
for loop
Character Description
String \t Tab
\n Newline
\r Carriage return
\” Double Quote
\’ Single Quote
\\ Backslash
● An array is a collection of data, each item in array has
an index to access it.
● Ways to use array in JavaScript
Array ● var myArray = new Array();
● myArray[0] = “darshan”;
● myArray[1] = 222;
● myArray[2] = false;
● var myArray = new Array(“darshan” , 123 , true);
● A JavaScript function is a block of code designed to
perform a particular task.
● A JavaScript function is executed when "something"
invokes it.
● A JavaScript function is defined with the function
keyword, followed by a name, followed by parentheses
().
● The parentheses may include parameter names
Function separated by commas: (parameter1, parameter2, ...)
● The code to be executed, by the function, is placed
inside curly brackets.
● Example :
function myFunction(p1, p2)
{
return p1 * p2;
}
● When JavaScript reaches a return statement, the
function will stop executing.
● If the function was invoked from a statement,
JavaScript will "return" to execute the code after the
Function invoking statement.
● The code inside the function will execute when
"something“ invokes (calls) the function:
● When an event occurs (when a user clicks a button)
● When it is invoked (called) from JavaScript code
● Automatically (self invoked)
● Popup boxes can be used to raise an alert, or to get
confirmation on any input or to have a kind of input
from the users.
Popup Boxes ● JavaScript supports three types of popup boxes.
● Alert box
● Confirm box
● Prompt box
● An alert box is used if you want to make sure
information comes through to the user.
● When an alert box pops up, the user will have to click
"OK" to proceed.
● It can be used to display the result of validation.
● Example:
Alert Box
<html>
<head>
<title>Alert Box</title>
</head>
<body>
<script>
alert("Hello World");
</script>
</body>
</html>
● A confirm box is used if you want the user to accept
something.
● When a confirm box pops up, the user will have to
click either"OK" or "Cancel" to proceed, If the user
clicks "OK", the box returns true. If the user clicks
"Cancel", the box returns false.
● Example :
Confirm Box
<script>
var a = confirm(“Are you sure??");
if(a==true) {
alert(“User Accepted”);
}
else {
alert(“User Cancled”);
}
</script>
● A prompt box is used if you want the user to input a
value.
● When a prompt box pops up, user have to click either
"OK" or"Cancel" to proceed, If the user clicks "OK" the
box returns the input value, If the user clicks "Cancel"
the box returns null.
Prompt Box
<script>
</script>
● We can create external JavaScript file and embed it in
many html pages.
● It provides code reusability because single JavaScript
file can be used in several html pages.
● An external JavaScript file must be saved by .js
External extension.
Javascript ● To embed the External JavaScript File to HTML we can
use script tag with src attribute in the head section to
specify the path of JavaScript file.
● For Example :
● <script type="text/javascript"
src="message.js"></script>
message.js
function myAlert(msg) {
External
Javascript
Myhtml.html
<html>
<head>
<script src=“message.js”></script>
</head>
<body>
<script> myAlert(“Hello World”); </script>
</body>
</html>
● An object is just a special kind of data, with properties
and methods.
Accessing Object Properties
● Properties are the values associated with an object.
● The syntax for accessing the property of an object is
Javascript below
Objects objectName.propertyName
● This example uses the length property of the
Javascript’s inbuilt
object(String) to find the length of a string:
var message="Hello World!";
var x=message.length;
● The Math object allows you to perform mathematical
tasks.
● The Math object includes several mathematical
Javascript constants and methods.
Objects: <script>
</script>
● Math object has some properties which are,
Properties Description
inbuilt acos(x)
asin(x)
Returns the arccosine of x, in radians
methods sqrt(x)
max(x,y, z,...,n)
Returns the square root of x
person={
firstname: “A",
eyecolor: "blue"
</script>
● A constructor is pre defined method that will initialize
your object.
● To do this in JavaScript a function is used that is
invoked through the new operator.
● Any properties inside the newly created object are
assigned using this keyword, referring to the current
object being created.
User defined <script>
Objects function person(firstname, lastname, age){
this.firstname = firstname;
this.lastname = lastname;
this. changeFirstName = function (name){ this.firstname = name };
}
var person1=new person("Narendra","Modi",50);
person1.changeFirstName(“NAMO”);
alert(person1.firstname + “ ”+ person1.lastname);
</script>
● The Document Object Model is a platform and
language neutral interface that will allow programs
and scripts to dynamically access and update the
content, structure and style of documents.
● The window object is the primary point from which
most other objects come.
Document ● From the current window object access and control
Object Model can be given to most aspects of the browser features
and the HTML document.
(DOM) ● When we write :
document.write(“Hello World”);
● We are actually writing :
window.document.write(“Hello World”);
● The window is just there by default
● This window object represents the window or frame
that displays the document and is the global object in
client side programming for JavaScript.
Document ● All the client side objects are connected to the window
object.
Object Model
(DOM)
Property Description
getElementBy
<script>
Id() <html> function myFunction()
<body> {
<input type=“text” id=“myText”> var txt =
<input type=“button” document.getElementById(“my
onClick=“myFunction()”> Text”);
</body> alert(txt.value);
</html> }
</script>
● When we suppose to get the reference of the
elements from HTML in JavaScript using name
specified in the HTML we can use this method.
● It will return the array of elements with the provided
name.
● Example :
getElementsB <html>
<script>
yName() <body>
function myFunction()
{
<input type=“text”
a=document.getElementsByNa
name=“myText”>
me(“myText”)[0];
<input type=“button”
alert(a.value);
onClick=“myFunction()”>
}
</body>
</script>
</html>
● When we suppose to get the reference of the
elements from HTML in JavaScript using name of the
tag specified in the HTML we can use this method.
● It will return the array of elements with the provided
tag name
● Example :
getElementsB
yTagName() <html> <script>
<body> function myFunction() {
<input type=“text” a=document.getElementsByTag
name=“uname”> Name(“input”);
<input type=“text” alert(a[0].value);
name=“pword”> alert(a[1].value);
</body> }
</html> </script>
● We can access the elements of form in DOM quite
easily using the name/id of the form.
● Example :
function f()
{
<html>
var a =
<body>
document.forms[“myForm”];
Forms using <form name=“myForm”>
<input type=“text”
var u = a.uname.value;
var p = a.pword.value;
DOM name=“uname”>
<input type=“text”
if(u==“admin” && p==“123”)
{
name=“pword”>
alert(“valid”);
<input type=“button”
}
onClick=“f()”>
else
</form>
{
</body>
alert(“Invalid”);
</html>
}
}
● Validation is the process of checking data against a
standard or requirement.
● Form validation normally used to occur at the server,
after client entered necessary data and then pressed
the Submit button.
● If the data entered by a client was incorrect or was
Validation simply missing, the server would have to send all the
data back to the client and request that the form be
resubmitted with correct information.
● This was really a lengthy process which used to put a
lot of burden on the server.
● JavaScript provides a way to validate form's data on
the client's computer before sending it to the web
server.
● Form validation generally performs two functions.
● Basic Validation
● Emptiness
● Confirm Password
● Length Validation etc……
Validation ● Data Format Validation
● Secondly, the data that is entered must be checked for
correct form and value.
● Email Validation
● Mobile Number Validation
● Enrollment Number Validation etc….
● A regular expression is an object that describes a
pattern of characters.
● Regular expressions are used to perform pattern-
matching and "search-and-replace" functions on text.
● Example:
var pattern = "^ [\\w]$"; // will allow only words in the
Validation string
using RegExp var regex = new RegExp(pattern);
If(regex.test(testString)){
//Valid
} else {
//Invalid
}
● To find word characters in the string we can use \w
● We can also use [a-z], [A-Z] or [a-zA-Z] for the same
● To find non-word characters in the string we can use \
Validation W
function checkMail()
<body>
<div id=“myDiv”>
</div>
<script>
DHTML var objDiv = document.getElementById(“myDiv”);
var nextColor = 0;
setInterval(“objDiv.style.backgroundColor =
colors[nextColor++%colors.length];”,500);
</script>
</body>
</html>
DHTML – ● DHTML, or Dynamic HTML, is really just a combination
of HTML, JavaScript and CSS.
Combining ● The main problem with DHTML, which was introduced
HTML,CSS & in the 4.0 series of browsers, is compatibility.
<script>
var a = document.getElementById(‘myImg’);
a.src = “xyz.jpg”;
</script>
Event Description