JavaScript JavaScript • JavaScript is a programming language used to make web pages interactive. • JavaScript is scripting language used for client side scripting. • JavaScript.

Download Report

Transcript JavaScript JavaScript • JavaScript is a programming language used to make web pages interactive. • JavaScript is scripting language used for client side scripting. • JavaScript.

Slide 1

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 45






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 2

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 44






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 3

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 43






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 4

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 42






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 5

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 41






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 6

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 40






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 7

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 39






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 8

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 38






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 9

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 37






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 10

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 36






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 11

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 35






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 12

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 34






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 13

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 33






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 14

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 32






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 15

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 31






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 16

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 30






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 17

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 29






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 18

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 28






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 19

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 27






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 20

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 26






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 21

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 25






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}


Enter first no :


Enter sec no:























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}


















Enter first no :name=first>


Enter sec no:name=sec>

onClick="add(f);">
onClick="sub(f);">
onClick="mult(f);">
onClick="div(f);">




Answer :














JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}


Enter limt no :




JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }


• Email:



JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example



<script language="javascript">
var colors = ["red", "green", "blue"];
for(i=0;i<3;i++)
document.write(colors[i]+" ");



















JavaScript II
Array Example



<script language="javascript">
var colors = new Array();
for(i=0;i<10;i++)
{
colors[i]="color"+i;
document.write(colors[i]+" ");
}




JavaScript

















JavaScript
File popo.js Contents:
function popup() {
alert("Hello popo")
}
-------------------------------------------------------------------------------------HTML & JavaScript Code:


<script src=“popo.js">

























JavaScript
Print Index of select

<script language="javascript">
function check(n){
var t=parseInt(n)+1;
alert("index is "+ t);
}






JavaScript

JavaScript
• javaScript Print Script - window.print()
• The JavaScript print function
window.print() will print the current
webpage when executed.




JavaScript













JavaScript Redirect

<script type="text/javascript">
function delayer(){
window.location = "http://www.poposir.orgfree.com"
}


This page is waiting for redirect location , poposir.orgfree.com"





The delayer() function to be used after 5 seconds or 5000 milliseconds , so
we pass the setTimeout() two arguments.
'delayer()' - The function we want setTimeout() to execute after the
specified delay.
5000 - the number of millisecods to wait before executing our function.
1000 miliseconds = 1 second.





















JavaScript
document.getElementById()
To quickly access the value of an HTML element.
This small script below will check to see if there is any text in the text field "myText".
The argument that getElementById requires is the id of the HTML element you wish to
utilize.
<script type="text/javascript">
function notEmpty(){
var myTextField = document.getElementById('myText');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
else
alert("Would you please enter some text?")
}

















JavaScript
Cuttent Time AM/ PM

It is now
<script type="text/javascript">
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){minutes = "0" + minutes;}
document.write(hours + ":" + minutes + " ");
if(hours > 11){document.write("PM");}
else {document.write("AM");}



JavaScript


Slide 22

JavaScript

JavaScript
• JavaScript is a programming language
used to make web pages interactive.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995
• Case sensitive

JavaScript

• Benefits of JavaScript
• JavaScript gives HTML designers a
programming tool
• JavaScript can react to events Alert
Messages
• JavaScript can be used to validate data
• JavaScript can be used to input Validation
• Disadvantages of JavaScript
• Depends on the browser

JavaScript

• Java and JavaScript
• Java and JavaScript are two completely
different
• Java (developed by Sun Microsystems) is a
powerful and much more complex
programming.
• JavaScript is scripting language used for
client side scripting.
• JavaScript developed by Netscape in 1995.

JavaScript

• Embedding JavaScript in HTML
• <script> tag is used to insert a JavaScript into
an HTML page


• <script language="javascript">
• document.write ("Hello");




JavaScript
• External file
• JavaScript can be put in a separate .js
file
<script
src="myJavaScriptFile.js">
An external .js file lets you use the same
JavaScript on multiple HTML pages










JavaScript


<script language="javascript">
document.write ("

Welcome to
popo! Hello");



document.write()- method used to print text














JavaScript
document.writeln() and \n
- used to print in new line(only valid when

 tag)
 (pre format)



<script language="javascript">
document.write ("

Welcome to popo!
\nHello");
document.writeln ("and peepe");




JavaScript
• lastModified
• include the last update date on your page
by using the following code:
• <script language="JavaScript">
• document.write("This page Last update:" +
document.lastModified);


JavaScript






bgColor and fgColor
<script>
document.bgColor="black“;
document.fgColor="#336699“;


JavaScript





Data Types
Basic types of data in JavaScript are
strings, numbers, boolean and null.
String- group of characters enclosed in
double quote
• Number- integers and floating point values
• Boolean- true or false
• Null- represents nothing and has a special
value, indicated by null

















JavaScript
Variables
Variables are storage locations used to store date
Variable can be declared as
var variable name= value;
Eg
Var example=“this is a string”;



<script language="javascript">
var name="popo";
document.writeln ("My name " +name);




JavaScript
• Operators
• JavaScript uses “operators” allows to make
mathematical calculations
• Assignment operators =, +=, -=, *=,/=
• Arithmetic operators
+, -, *, /, %
• Logical operators
&&, || , !=
• Comp`arison operators <, <=, >, >=, ==, !=
• Conditional operator (exp1)?(exp2) : (exp3);













JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
document.writeln ("sum of "+a+" + "+b+" = "+ c);

















JavaScript



<script language="javascript">
var a=10;
var b=20;
var c=a+b;
if(c%2==0){document.write("even");}
else{document.write("odd");}




JavaScript

• loop statements:
while (condition) statement;
do statement while (condition);
for (initialization; condition; increment) statement;















<script language="javascript">
var i;
for(i=1;i<=40;i++)
document.writeln(i);




JavaScript

• The switch statement:

switch (expression)
• {
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}














JavaScript
Alert() method
Used to alert the user on some action, with some information
Syn
alert(“message”);



<script language="javascript">
alert("I am popo");





















JavaScript
Prompt() Method
Prompt method displays a small dialog box with a provision for text entry
along with 2 buttons
Ok and Cancel
The text entered in the box can be stored in a variable



<script language="javascript">
var age= prompt("enter age");
if(age>=20)alert("I am happy");
else alert("I am popo");



Can assign value
var k=prompt("dfgsdf",“value");














JavaScript
Confirm() method
Enables the user to confirm



<script language="javascript">
var age=prompt("enter age");
if( confirm("is it greater than 20") )alert("I am happy");
else alert("I am popo");





















JavaScript
parseInt()- convert string to integer
parseFloat()-convert string to float
var a= parseInt(b)+parseInt( c);



<script language="javascript">
var a="10";
var b="20";
var c=a+b;
alert(c);
var d=parseInt(a)+parseInt(b);
alert(d);




JavaScript
• eval() method
• evaluate a numeric expression given as a
string into numerical value
• Eg
• Eval(“10*10”); 100

JavaScript
• Date function
• Date manipulations can be performed by the
method of the Date object
• Create an instance of Date object
• Using different methods of Date object user
can carry out date manipulations
• Some methods are

JavaScript
• Date methods
getDate

Return day of the month as integer(1-31)

getDay

Return day of the week as integer(0-6)

getMonth

Return month as integer (0-11)

getSeconds Return seconds as integers (0-59)
getYear

Return year as two digit integer

setDate

set the day of the month as integer(1-31)

JavaScript





<script language="javascript">

var d=new Date();
• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());





• Output -10/01/2013






JavaScript
Functions
Function can be defined
function function name(arguments)
{
– Function body;











}
Eg
function fact(num)
{
var fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}













JavaScript




















JavaScript






The function returned 25.

JavaScript
• Event handler
• Event handlers can be considered as
triggers that execute JavaScript when
something happens, such as click or move
your mouse over a link, submit a form etc.
• Events are signals generated when specific
action occur.
Script can be written to react to these
events.

JavaScript
onclick
ondblclick
onmousedown
onmousemove
onmouseout
onmouseover
onmouseup
onkeydown
onkeypress
onchange
onreset
onsubmit
onfocus
onblur
onload
unload

Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs
Occurs

when user clicks on link or form element
when a mouse double-click
when mouse button is pressed
when mouse pointer moves
when mouse pointer moves out of an element
when mouse pointer moves over an element
when mouse button is released
when a key is pressed
key is pressed and released
when user changes the value in form field
when a form is reset
when a form is submitted
when user clicks inside the field
when user clicks outside a field
when a page is loaded
when user leaves a page

JavaScript

• onClick
• onClick handlers execute only when users click on buttons,
links, etc.
• <script>
• function ss()
• {
• alert("Thank you popo!")
• }




• The function ss() is invoked when the user clicks the button.
• Note: Event handlers are not added inside the <script> tags













JavaScript
onDblclick
Occurs when a mouse double-click
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousedown
Occurs when mouse button is pressed
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onMousemove
Occurs when mouse pointer moves
<script>
function ss()
{
alert("Thank you popo!")
}



















JavaScript
onMouseout
Occurs when mouse pointer moves out of an element
<script>
function ss()
{
alert("Thank you popo!")
}




popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:






























































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :






































Enter limt no :•



















• Email:•





































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •

























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 24






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •










































































































popo














JavaScript
onkeypress
Occurs key is pressed and released
<script>
function ss()
{
alert("Thank you popo!")
}

















JavaScript
onsubmit
Occurs when a form is submitted
<script>
function ss()
{
alert("Thank you popo!")
}















JavaScript
onload
Occurs when a page is loaded
<script>
function ss()
{
alert("popo");
}













JavaScript
onmouseover
Occurs when mouse pointer moves over an element
<script>
function ss()
{
document.write("popo");
}

Over Here!

JavaScript
• unload
• Occurs when user leaves a page


JavaScript
• Most of the events handlers are associated with
the following object
Object
Selectionlist
Text element
Textarea element
Button element
Checkbox
Radio button
Hyper link
Submit button
Reset button
Document
Window
Form

Event Handlers
onBlur, onChange, onFocus
onBlur, onChange, onFocus, onSelect
onBlur, onChange, onFocus, onSelect
onClick
onClick
onClick
onClick, onMouseover
onClick
onClick
onLoad, onUnload
onLoad, onUnload
onSubmit

















JavaScript
Addition of 2 nos (each Nos in 2 text, result in result text, with a button)
<script>
function calcu(f)
{
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);
}

Enter first no :Enter sec no:




























JavaScript
All arithmetic operation with 4 buttons & 3 text
<script>
function add(f){
f.ans.value=parseInt(f.first.value)+parseInt(f.sec.va
lue);
}
function sub(f){
f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);
}
function mult(f){
f.ans.value=parseInt(f.first.value)*parseInt(
f.sec.value);
}
function div(f){
f.ans.value=parseInt(f.first.value)/parseInt(
f.sec.value);
}

















Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :





























JavaScript
Print First n nos
<script>
function pr(f)
{
var n=parseInt(f.no.value);
for(i=1;i<=n;i++)
document.write(i+"   ");
}

Enter limt no :•






JavaScript

JavaScript

• String length
• Var str="Hello world!"
document.write(str.length);
• -----------------------------------------------------------------------• <script>
• function s(f)
• {
• st=f.st.value
• alert(st.length);
• }

• Email:•





JavaScript

• charAt() function to get character from a location
inside a string
• var my_str="Welcome “
document.write(my_str.charAt(3) );
• The output of above code is c
.
concat() join 2 strings
• Var var2=" popo"
var var3= " pp"
var var4=var1.concat(var2,var3);
document.write(var4);







JavaScript

indexOf() to get location of a string
var my_str="popo and pp"
document.write( my_str.indexOf("and") ) ;
Output 5
.
lastIndexOf() This function returns the position
of string by checking from end or right side of
the string
• var my_str="popo and pp"
• document.write( my_str.lastIndexOf("a") ) ;
• Output 5
.

JavaScript
• Search & replace of part of string by
replace() method
• var msg="Welcome to popo";
• msg=msg.replace("popo","Ajith");
• document.write(msg);
• Output : Welcome to Ajith

JavaScript
• Substring()
• In substr() function we used start point and
length of the substring required
• my_string= new String("Welcome to popo");
• document.write(my_string.substring(2,5));
• Output : lco

JavaScript












JavaScript
String reversal using charat and length property
<script type="text/javascript">
var my_str="nattop"
var i=my_str.length;
i=i-1;
for (var x = i; x >=0; x--)
{
document.write(my_str.charAt(x));
}


JavaScript

• String Search
• WE can search for presence of a string inside a main string in
client side JavaScript by using string.search function.
• If the search string is found inside the main string then it will
return the position of the location of the string.
• If the string is not found inside the main string then it returns 1.
• <script type="text/javascript">
• var my_str="Welcome to popo"
• var str="popo";
• document.write (my_str.search(str));

• Output 11









JavaScript
Lower case text to Uppercase
var a1 = str.toUpperCase();
Uppercase letters to Lower case
var a2 = str.toLowerCase();
Checking number using isNaN function
isNaN() to check any data is number or string
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

JavaScript





Number to string
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();

















JavaScript

Check validation
<script>
function validate()
{
if(document.login.userName.value=="")
{ alert ("Please enter User Name") }
if(document.login.password.value=="")
{ alert ("Please enter Password") }
}







JavaScript

JavaScript II

• Array
• Different ways to declare Array
• 1) var colors = ["red", "green", "blue"];


colors[0] is "red"

• 2) new Array() - to create an empty array:
– var colors = new Array();
– Add elements to the array later:
colors[0] = "red";
– colors[1]="green"; colors[2] = "blue";

• 3) new Array(n) to create an array of that size
– var colors = new Array(3);













JavaScript II
Array example


<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 23






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 46






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 47






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 48






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 49






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 50






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 51






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 52






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 53






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 54






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 55






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 56






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 57






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 58






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 59






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 60






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 61






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 62






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 63






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 64






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 65






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 66






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 67






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 68






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScriptJavaScriptJavaScript• JavaScript is a programming languageused to make web pages interactive.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995• Case sensitiveJavaScript• Benefits of JavaScript• JavaScript gives HTML designers aprogramming tool• JavaScript can react to events AlertMessages• JavaScript can be used to validate data• JavaScript can be used to input Validation• Disadvantages of JavaScript• Depends on the browserJavaScript• Java and JavaScript• Java and JavaScript are two completelydifferent• Java (developed by Sun Microsystems) is apowerful and much more complexprogramming.• JavaScript is scripting language used forclient side scripting.• JavaScript developed by Netscape in 1995.JavaScript• Embedding JavaScript in HTML• <script> tag is used to insert a JavaScript intoan HTML page• • • <script language="javascript">• document.write ("Hello");• • • JavaScript• External file• JavaScript can be put in a separate .jsfile<scriptsrc="myJavaScriptFile.js">An external .js file lets you use the sameJavaScript on multiple HTML pages••••••••JavaScript<script language="javascript">document.write ("





















































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















Slide 69






































































Welcome topopo! Hello");document.write()- method used to print text ••••••••••••JavaScriptdocument.writeln() and \n- used to print in new line(only valid when





















 tag) (pre format)



<script language="javascript">document.write ("


Welcome to popo!\nHello");document.writeln ("and peepe"); JavaScript• lastModified• include the last update date on your pageby using the following code:• <script language="JavaScript">• document.write("This page Last update:" +document.lastModified);• JavaScript•••••bgColor and fgColor<script>document.bgColor="black“;document.fgColor="#336699“; JavaScript••••Data TypesBasic types of data in JavaScript arestrings, numbers, boolean and null.String- group of characters enclosed indouble quote• Number- integers and floating point values• Boolean- true or false• Null- represents nothing and has a specialvalue, indicated by null •••••••••••••••JavaScriptVariablesVariables are storage locations used to store dateVariable can be declared asvar variable name= value;EgVar example=“this is a string”;






































































<script language="javascript">var name="popo";document.writeln ("My name " +name); JavaScript• Operators• JavaScript uses “operators” allows to makemathematical calculations• Assignment operators =, +=, -=, *=,/=• Arithmetic operators+, -, *, /, %• Logical operators&&, || , !=• Comp`arison operators <, <=, >, >=, ==, !=• Conditional operator (exp1)?(exp2) : (exp3); •••••••••••JavaScript


































<script language="javascript">var a=10;var b=20;var c=a+b;document.writeln ("sum of "+a+" + "+b+" = "+ c);••••••••••••JavaScript


























<script language="javascript">var a=10;var b=20;var c=a+b;if(c%2==0){document.write("even");}else{document.write("odd");}JavaScript• loop statements:while (condition) statement;do statement while (condition);for (initialization; condition; increment) statement;••••••••••































<script language="javascript">var i;for(i=1;i<=40;i++)document.writeln(i);JavaScript• The switch statement:switch (expression)• {case label :statement;break;case label :statement;break;...default : statement;}••••••••••••JavaScriptAlert() methodUsed to alert the user on some action, with some informationSynalert(“message”);













































<script language="javascript">alert("I am popo");•••••••••••••••JavaScriptPrompt() MethodPrompt method displays a small dialog box with a provision for text entryalong with 2 buttonsOk and CancelThe text entered in the box can be stored in a variable
































<script language="javascript">var age= prompt("enter age");if(age>=20)alert("I am happy");else alert("I am popo");Can assign valuevar k=prompt("dfgsdf",“value");••••••••••••JavaScriptConfirm() methodEnables the user to confirm





























<script language="javascript">var age=prompt("enter age");if( confirm("is it greater than 20") )alert("I am happy");else alert("I am popo");••••••••••••••••JavaScriptparseInt()- convert string to integerparseFloat()-convert string to floatvar a= parseInt(b)+parseInt( c);
































<script language="javascript">var a="10";var b="20";var c=a+b;alert(c);var d=parseInt(a)+parseInt(b);alert(d);JavaScript• eval() method• evaluate a numeric expression given as astring into numerical value• Eg• Eval(“10*10”); 100JavaScript• Date function• Date manipulations can be performed by themethod of the Date object• Create an instance of Date object• Using different methods of Date object usercan carry out date manipulations• Some methods areJavaScript• Date methodsgetDateReturn day of the month as integer(1-31)getDayReturn day of the week as integer(0-6)getMonthReturn month as integer (0-11)getSeconds Return seconds as integers (0-59)getYearReturn year as two digit integersetDateset the day of the month as integer(1-31)JavaScript• • • 























































•<script language="javascript">•var d=new Date();• document.write(d.getDate()+"/"+d.getMonth()+1 +"/"+d.getYear());•• • • Output -10/01/2013••••JavaScriptFunctionsFunction can be definedfunction function name(arguments){– Function body;•••••••••}Egfunction fact(num){var fact=1;for(i=1;i<=num;i++)fact=fact*i;return fact;}•••••••••••JavaScript••••••••••••JavaScriptThe function returned 25.JavaScript• Event handler• Event handlers can be considered astriggers that execute JavaScript whensomething happens, such as click or moveyour mouse over a link, submit a form etc.• Events are signals generated when specificaction occur.Script can be written to react to theseevents.JavaScriptonclickondblclickonmousedownonmousemoveonmouseoutonmouseoveronmouseuponkeydownonkeypressonchangeonresetonsubmitonfocusonbluronloadunloadOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccursOccurswhen user clicks on link or form elementwhen a mouse double-clickwhen mouse button is pressedwhen mouse pointer moveswhen mouse pointer moves out of an elementwhen mouse pointer moves over an elementwhen mouse button is releasedwhen a key is pressedkey is pressed and releasedwhen user changes the value in form fieldwhen a form is resetwhen a form is submittedwhen user clicks inside the fieldwhen user clicks outside a fieldwhen a page is loadedwhen user leaves a pageJavaScript• onClick• onClick handlers execute only when users click on buttons,links, etc.• <script>• function ss()• {• alert("Thank you popo!")• }• • • The function ss() is invoked when the user clicks the button.• Note: Event handlers are not added inside the <script> tags•••••••••••JavaScriptonDblclickOccurs when a mouse double-click<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousedownOccurs when mouse button is pressed<script>function ss(){alert("Thank you popo!")}•••••••••••JavaScriptonMousemoveOccurs when mouse pointer moves<script>function ss(){alert("Thank you popo!")}•••••••••••••JavaScriptonMouseoutOccurs when mouse pointer moves out of an element<script>function ss(){alert("Thank you popo!")}

































































































































































• •











































































































popo •••••••••••JavaScriptonkeypressOccurs key is pressed and released<script>function ss(){alert("Thank you popo!")} •••••••••••JavaScriptonsubmitOccurs when a form is submitted<script>function ss(){alert("Thank you popo!")} •••••••••JavaScriptonloadOccurs when a page is loaded<script>function ss(){alert("popo");} •••••••••JavaScriptonmouseoverOccurs when mouse pointer moves over an element<script>function ss(){document.write("popo");} JavaScript• unload• Occurs when user leaves a page• JavaScript• Most of the events handlers are associated withthe following objectObjectSelectionlistText elementTextarea elementButton elementCheckboxRadio buttonHyper linkSubmit buttonReset buttonDocumentWindowFormEvent HandlersonBlur, onChange, onFocusonBlur, onChange, onFocus, onSelectonBlur, onChange, onFocus, onSelectonClickonClickonClickonClick, onMouseoveronClickonClickonLoad, onUnloadonLoad, onUnloadonSubmit •••••••••••••••JavaScriptAddition of 2 nos (each Nos in 2 text, result in result text, with a button)<script>function calcu(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);} ••••••••••••••JavaScriptAll arithmetic operation with 4 buttons & 3 text<script>function add(f){f.ans.value=parseInt(f.first.value)+parseInt(f.sec.value);}function sub(f){f.ans.value=parseInt(f.first.value)parseInt(f.sec.value);}function mult(f){f.ans.value=parseInt(f.first.value)*parseInt(f.sec.value);}function div(f){f.ans.value=parseInt(f.first.value)/parseInt(f.sec.value);}•••••••••••• •••••••••••JavaScriptPrint First n nos<script>function pr(f){var n=parseInt(f.no.value);for(i=1;i<=n;i++)document.write(i+"   ");} JavaScript JavaScript• String length• Var str="Hello world!"document.write(str.length);• -----------------------------------------------------------------------• <script>• function s(f)• {• st=f.st.value• alert(st.length);• }• • • JavaScript• charAt() function to get character from a locationinside a string• var my_str="Welcome “document.write(my_str.charAt(3) );• The output of above code is c.concat() join 2 strings• Var var2=" popo"var var3= " pp"var var4=var1.concat(var2,var3);document.write(var4); •••••JavaScriptindexOf() to get location of a stringvar my_str="popo and pp"document.write( my_str.indexOf("and") ) ;Output 5.lastIndexOf() This function returns the positionof string by checking from end or right side ofthe string• var my_str="popo and pp"• document.write( my_str.lastIndexOf("a") ) ;• Output 5. JavaScript• Search & replace of part of string byreplace() method• var msg="Welcome to popo";• msg=msg.replace("popo","Ajith");• document.write(msg);• Output : Welcome to Ajith JavaScript• Substring()• In substr() function we used start point andlength of the substring required• my_string= new String("Welcome to popo");• document.write(my_string.substring(2,5));• Output : lco JavaScript ••••••••••JavaScriptString reversal using charat and length property<script type="text/javascript">var my_str="nattop"var i=my_str.length;i=i-1;for (var x = i; x >=0; x--){document.write(my_str.charAt(x));} JavaScript• String Search• WE can search for presence of a string inside a main string inclient side JavaScript by using string.search function.• If the search string is found inside the main string then it willreturn the position of the location of the string.• If the string is not found inside the main string then it returns 1.• <script type="text/javascript">• var my_str="Welcome to popo"• var str="popo";• document.write (my_str.search(str));• • Output 11 •••••••JavaScriptLower case text to Uppercasevar a1 = str.toUpperCase();Uppercase letters to Lower casevar a2 = str.toLowerCase();Checking number using isNaN functionisNaN() to check any data is number or stringvar my_string="This is a string";if(isNaN(my_string)){document.write ("this is not a number ");}else{document.write ("this is a number ");} JavaScript••••Number to stringTo convert a number to a string, do:var c = (16 * 24)/49 + 12;d = c.toString(); •••••••••••••••JavaScriptCheck validation<script>function validate(){if(document.login.userName.value==""){ alert ("Please enter User Name") }if(document.login.password.value==""){ alert ("Please enter Password") }} JavaScript JavaScript II• Array• Different ways to declare Array• 1) var colors = ["red", "green", "blue"];•colors[0] is "red"• 2) new Array() - to create an empty array:– var colors = new Array();– Add elements to the array later:colors[0] = "red";– colors[1]="green"; colors[2] = "blue";• 3) new Array(n) to create an array of that size– var colors = new Array(3); •••••••••••JavaScript IIArray example




























































































Over Here!





























































Enter first no :Enter sec no:































































Enter first no :name=first>Enter sec no:name=sec>onClick="add(f);">onClick="sub(f);">onClick="mult(f);">onClick="div(f);">Answer :







































Enter limt no :•




















• Email:•






































































































































































































<script language="javascript">var colors = ["red", "green", "blue"];for(i=0;i<3;i++)document.write(colors[i]+" ");••••••••••••••JavaScript IIArray Example




























<script language="javascript">var colors = new Array();for(i=0;i<10;i++){colors[i]="color"+i;document.write(colors[i]+" ");}JavaScript•••••••••••••••JavaScriptFile popo.js Contents:function popup() {alert("Hello popo")}-------------------------------------------------------------------------------------HTML & JavaScript Code:<script src=“popo.js">•••••••••••••••••JavaScriptPrint Index of select<script language="javascript">function check(n){var t=parseInt(n)+1;alert("index is "+ t);}JavaScriptJavaScript• javaScript Print Script - window.print()• The JavaScript print functionwindow.print() will print the currentwebpage when executed.• JavaScript•••••••••••JavaScript Redirect<script type="text/javascript">function delayer(){window.location = "http://www.poposir.orgfree.com"}JavaScript
			


			
		
	


















































































































• •


























This page is waiting for redirect location , poposir.orgfree.com"•The delayer() function to be used after 5 seconds or 5000 milliseconds , sowe pass the setTimeout() two arguments.'delayer()' - The function we want setTimeout() to execute after thespecified delay.5000 - the number of millisecods to wait before executing our function.1000 miliseconds = 1 second.••• •••••••••••••••JavaScriptdocument.getElementById()To quickly access the value of an HTML element.This small script below will check to see if there is any text in the text field "myText".The argument that getElementById requires is the id of the HTML element you wish toutilize.<script type="text/javascript">function notEmpty(){var myTextField = document.getElementById('myText');if(myTextField.value != "")alert("You entered: " + myTextField.value)elsealert("Would you please enter some text?")} ••••••••••••JavaScriptCuttent Time AM/ PM



































































It is now<script type="text/javascript">var currentTime = new Date();var hours = currentTime.getHours();var minutes = currentTime.getMinutes();if (minutes < 10){minutes = "0" + minutes;}document.write(hours + ":" + minutes + " ");if(hours > 11){document.write("PM");}else {document.write("AM");}
















  • Company
  • Nicosia Constantinou Palaiologou 16, Palouriotissa, 1040
  • +357 64-733-402
  • [email protected]
  • Links
  • About
  • Contact
  • Help / FAQ
  • Legal
  • Terms of Service
  • Privacy policy
  • Cookie policy
  • Disclaimer

    slideum.com © 2025, Inc. All rights reserved.