Unitiii Client Sideprocessingandscripting 230517110456 7483c174
Unitiii Client Sideprocessingandscripting 230517110456 7483c174
Kaviya.P
Kamaraj College of Engineering & Technology
Unit III – Client-Side Processing and Scripting
• HTML objects such as buttons and text boxes are equipped to support
event handlers. Event handlers are functions that are invoked in response to
a message generated by the system or user.
JavaScript – Header Script
<html >
<head>
<title>Printing Multiple Lines in a Dialog Box</title>
<script type = "text/javascript">
<! --
document.write( "Welcome to <br/> JavaScript Programming!" );
// -->
</script>
</head>
<body>
</body>
</html>
• Adding // in front of a code line changes the code lines from an executable
line to a comment.
JavaScript – Variables
Variables are containers for storing data (storing data values).
Example:
var x = 5;
var y = 6;
var z = x + y;
JavaScript – Variables
String: var s = “india”
• The typeof keyword can be used to check the current variable type.
JavaScript – Variables
JavaScript Variable Scope
Example:
<html>
<body onload = checkscope();>
<script type = "text/javascript">
<!-- var myVar = "global"; // Declare a global variable
function checkscope( ) {
var myVar = "local"; // Declare a local variable
document.write(myVar); }
//-->
</script>
</body>
</html>
JavaScript – Reserved Words
abstract else instanceof switch
boolean enum int synchronized
break export interface this
byte extends long throw
case false native throws
catch final new transient
char finally null true
class float package try
const for private typeof
continue function protected var
debugger goto public void
default if return volatile
delete implements short while
do import static with
double in super
JavaScript – Data Types
• String: Sequence of characters enclosed in a set of single or double quotes
• Bigint: Used to store integer values that are too big to be represented by a normal
JavaScript Number. Ex: let x = BigInt("123456789012345678901234567890");
• Boolean: Either true/false or a number (0 being false) can be used for Boolean
values
• Undefined: Is a special value assigned to an identifier after it has been declared but
before a value has been assigned to it
JavaScript – Literals
For string, escape sequence can be used to embed special values. An escape sequence con
the back slash character followed by a character that has special meaning. Escape sequen
• Literals arerecognized by JavaScript include:
simple constants.
‘/nTitle/n’ \t tab
\\ backslash character
true
\" double quote
\‟ Single quote
\ddd Octal number
\xdd Tow digit hexadecimal number
\xdddd Four digit hexadecimal number
JavaScript – Control Statements
Statement Syntax Example
if (condition) if (a>b)
statement; document.write(“a is greater than b”);
if-else
else else
statement; document.write(“b is greater than a”);
while(condition) { while(i<5) {
statements; i=i+1;
while
} document.write(“value of I”+i);
}
do { do{
Statements; i=i+1;
do…while
} while(condition); document.write(“value of I”+i)
}while(i<5);
JavaScript – Control Statements
Statement Syntax Example
for(initialization; testcondition;
for(i=0; i<5; i++){
stepcount) {
for document.write(i);
Statements;
}
}
switch(expression) {
switch(choice)
case 1:statements
{
break;
case 1:c=a+b;
case 2: statements
Switch…Case break;
break;
case 2:c=a-b;
….
break;
default: statements
}
}
JavaScript – Control Statements
Statement Syntax Example
for(i=10;i>=0;i--) {
if(i==5)
break: break;
break;
}
for(i=10;i>=0;i--) {
if(i==5)
{
Continue: continue;
X=i;
continue;
}
JavaScript – Control Statements
Statement Syntax Example
for(i=10;i>=0;i--) {
if(i==5)
break: break;
break;
}
for(i=10;i>=0;i--) {
if(i==5)
{
Continue: continue;
X=i;
continue;
}
JavaScript – Functions
• A JavaScript function is a block of code designed to perform a particular
task.
Syntax
<script type = "text/javascript">
…..
statements
</script>
JavaScript – Functions
Example
JavaScript – Functions
Example – Return Value
JavaScript – Functions
Example – Passing Parameters
JavaScript – Functions
Global Functions
• The top-level function in JavaScript that are independent of any specific object.
These functions use the built-in objects.
• encodeURI(uri): Used to encode the URI. This function encodes special characters
except , / ? : @ & = + $ #.
• Array Initialization
Properties Description
constructor Returns a reference to the array function that created the object.
Properties Description
concat() Returns a new array comprised of this array joined with other
array(s) and/or value(s)
every() Returns true if every element in this array satisfies the provided
testing function.
filter() Creates a new array with all of the elements of this array for which
the provided filtering function returns true.
forEach() Calls a function for each element in the array.
indexOf() Returns the first (least) index of an element within the array equal to
the specified value, or -1 if none is found.
join() Joins all elements of an array into a string.
lastIndexOf() Returns the last (greatest) index of an element within the array equal
to the specified value, or -1 if none is found.
JavaScript – Arrays
• Array Methods:
Properties Description
map() Creates a new array with the results of calling a provided function on
every element in this array.
pop() Removes the last element from an array and returns that element.
push() Adds one or more elements to the end of an array and returns the new
length of the array.
reduce() Apply a function simultaneously against two values of the array (from
left-to-right) as to reduce it to a single value.
reduceRight() Apply a function simultaneously against two values of the array (from
right-to-left) as to reduce it to a single value.
reverse() Reverses the order of the elements of an array
shift() Removes the first element from an array and returns that element.
slice() Extracts a section of an array and returns a new array.
JavaScript – Arrays
• Array Methods:
Properties Description
some() Returns true if at least one element in this array satisfies the provided
testing function
toSource() Represents the source code of an object
sort() Sorts the elements of an array
splice() Adds and/or removes elements from an array
toString() Returns a string representing the array and its element
Unshift() Adds one or more elements to the front of an array and returns the
new length of the array
JavaScript – Arrays
Write a JavaScript to print the largest and smallest values among 10 elements of an
array.
JavaScript – Arrays
Write a JavaScript to print the largest and smallest values among 10 elements of an
array.
JavaScript – Document Object Modeling
• Defining the standard for accessing and manipulating HTML, XML and
other scripting languages.
Method Description
getElementById Used to obtain the specific element which specified by some id
within the script
createElement Used to create an element node
createTextNode Used for creating text node
createAttribute Used for creating attribute
appendChild For adding a new child to specified node
removeChild For removing a new child to specified node
getAttribute To return a specified attribute value
setAttribute To set or change the specified attribute to the specified value
JavaScript – Document Object Modeling
DOM Properties
Property Description
• Syntax:
• All the properties and methods of Math are static and can be called by
using Math as an object without creating it.
• Example
document.write(math.min(3,4,5));
document.write(math.sin(30));
JavaScript – Build-in Objects
Math Methods
Methods Description
abs(num) Returns the absolute value of a number
sin(num), cos(num),
Returns the sine, cosine, tangent() of a number
tan(num)
min(a,b), max(a,b) Returns the smallest / largest of zero or more numbers
round(num) Returns the value of a number rounded to the nearest integer
random() Returns a pseudo-random number between 0 and 1
exp(num) Returns EN, where N is the argument, and E is Euler's
constant, the base of the natural logarithm
ceil(num), floor(num) Returns the largest integer less than or equal to a number
pow(a,b) Returns base to the exponent power, that is, base exponent
sqrt(num) Returns the square root of a number
log(num) Returns the natural logarithm (base E) of a number
JavaScript – Build-in Objects
Number Object
• Represents numerical data, either integers or floating-point numbers.
• Syntax:
• Properties
Methods Description
MAX_VALUE Display largest possible number
MIN_VAUE Display smallest possible number
NaN Display NaN, When not a number
PI Display the value PI
POSITIVE_INFINITY Display the positive infinity
NAGATIVE_INFINITY Display the negative infinity
JavaScript – Build-in Objects
Date Object
• Used to obtain the date and time.
• Syntax:
• A collection of characters
• It wraps Javascript's string primitive data type with a number of helper methods.
• Syntax:
Methods Description
substring(begin, end) Returns the substring specified by begin and end character
Methods Description
• It is a logical expression.
• Syntax:
Methods Description
exec Tests for a match in a string. If it finds a match, it returns a result
array, otherwise it returns null.
test Tests for a match in a string. If it finds a match, it returns true,
otherwise it returns false.
match Tests for a match in a string. It returns an array of information or null
on a mismatch.
search Tests for a match in a string. It returns the index of the match, or -1 if
the search fails.
replace Tests for a match in a string and replaces the matched substring with
a replacement substring.
split Uses the regular expression or a fixed string to break a string into an
array of substring.
JavaScript – Regular Expression
JavaScript – Regular Expression
JavaScript – Regular Expression
JavaScript – Regular Expression
JavaScript – Exceptions
• Exception handling is a mechanism that handles the runtime errors so that
the normal flow of the application can be maintained.
Statements:
• finally: Execute code, after try and catch, regardless of the result.
JavaScript – Exceptions
Syntax
try {
catch(err) {
}
JavaScript – Exceptions
JavaScript – Exceptions
JavaScript – Exceptions
JavaScript – Event Handling
Syntax:
Tag attribute
Event handler
JavaScript – Event Handling
• These widgets used in JavaScript are Textbox, Radio button, Check box
and so on.
b) Email ID validation.
c) Numbers and special characters not allowed in First name and Last name.
JavaScript – Debuggers
• The standard traditional method of debugging the JavaScript is using
alert() to display the values of the corresponding variables.
2. Use JavaScript and HTML to create a page with two panes. The first pane (on the
left) should have a text area where HTML code can be typed by the user. The
pane on the right side should display the preview of the HTML code typed by the
user, as it would be seen in the browser.