html body { margin-top: 50px !important; } #top_form { position: fixed; top:0; left:0; width: 100%; margin:0; z-index: 2100000000; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -o-user-select: none; border-bottom:1px solid #151515; background:#FFC8C8; height:45px; line-height:45px; } #top_form input[name=url] { width: 550px; height: 20px; padding: 5px; font: 13px "Helvetica Neue",Helvetica,Arial,sans-serif; border: 0px none; background: none repeat scroll 0% 0% #FFF; }
Module 3 - Java Script: Client Side Scripting
Module 3 - Java Script: Client Side Scripting
Dr.V.Mareeswari
Assistant Professsor (Sr)
School of Information Technology and Engineering
VIT University
5 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 6 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
Comments Variables
Single line comments start with // Variables are "containers" for storing information.
Multi line comments start with /* and end with */ Rules for JavaScript variable names:
Variable names are case sensitive (y and Y are two different variables)
Variable names must begin with a letter, the $ character, or the
underscore character
Declaring (Creating) JavaScript Variables
var x;
var carname="Volvo";
9 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 10 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
Operators
Local JavaScript Variables Arithmetic Operators: + - * / % ++ --
A variable declared within a JavaScript function becomes LOCAL and Assignment operators: = += -= *= /= %=
can only be accessed within that function. (the variable has local scope). To add two or more string variables together, use the + operator.
Local variables are destroyed when you exit the function. txt1="What a very";
txt2="nice day";
Global JavaScript Variables txt3=txt1+” “ +txt2;
Variables declared outside a function become GLOBAL, and all scripts Comparison Operators : == != > >= < <=
and functions on the web page can access it. === (is exactly equal to (value and type) )
Global variables are destroyed when you close the page. Logical Operators : && || !
If you declare a variable, without using "var", the variable always Conditional Operator : ? :
becomes GLOBAL.
11 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 12 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
If...Else Statements
Output ??? <html><body> <html><body>
<script type="text/javascript"> <script type="text/javascript">
<html> x=5+"5"; var d = new Date(); var r=Math.random();
<body> document.write(x); var time = d.getHours();
if (time<10) if (r>0.5)
<script type="text/javascript"> document.write("<br />"); { {
var x; x="5"+5; document.write("<b>Good document.write(“
morning</b>"); <a href=‘http://www.w3schools.com’>
x=5+5; document.write(x); } Learn Web Development!</a>");
else if (time>=10 && time<16)
document.write(x); document.write("<br />"); { }
document.write("<br />"); </script> document.write("<b>Good else
day</b>"); {
x="5"+"5"; <p>The rule is: If you add a }
document.write("<a
number and a string, the result else
document.write(x); { href=‘ftp://192.168.2.172/lab/’>Vis
document.write("<br />"); will be a string.</p> document.write("<b>Hello it FTP Data!</a>");
</body> World!</b>"); }
} </script></body></html>
</html> </script> </body></html>
13 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 14 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
Switch Statement
<html><body> For Loop
<script type="text/javascript"> <html><body> <html><body>
var d=new Date(); <script type="text/javascript"> <script type="text/javascript">
var theDay=d.getDay(); //Note that Sunday=0,Monday=1, var i=0; for (i = 1; i <= 6; i++)
etc. for (i=0;i<=5;i++)
{ {
switch (theDay)
document.write("The number is document.write("<h" + i +
{ ">This is heading " + i);
" + i);
case 5: document.write("Finally Friday"); break;
document.write("<br >"); document.write("</h" + i +
case 6: document.write("Super Saturday"); break; ">");
}
case 0: document.write("Sleepy Sunday"); break; </script></body></html> }
default: document.write("I'm looking forward to this weekend!");
} </script></body></html>
</script></body><html>
15 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 16 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
<html>
Popup Boxes
<head> JavaScript has three kind of popup boxes:
<script type="text/javascript">
1. Alert Box
function product(a,b)
{ 2. Confirm Box
return a*b; 3. Prompt box
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
</body>
</html>
21 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 22 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
25 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 26 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
27 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 28 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
Example:
var txt = "Hello World!";
document.write("<p>Big: " + txt.big() + "</p>"); Date Object
document.write("<p>Small: " + txt.small() + "</p>"); The Date object is used to work with dates and times.
document.write("<p>Bold: " + txt.bold() + "</p>"); var d = new Date();
document.write("<p>Italic: " + txt.italics() + "</p>"); getDate() Returns the day of the month (from 1-31)
document.write("<p>Strike: " + txt.strike() + "</p>"); getDay() Returns the day of the week (from 0-6)
document.write("<p>Fontcolor: " + txt.fontcolor("green") + getYear()Returns the year (four digits)
"</p>");
getHours()Returns the hour (from 0-23)
document.write("<p>Fontsize: " + txt.fontsize(6) + "</p>");
getMinutes()Returns the minutes (from 0-59)
document.write("<p>Subscript: " + txt.sub() + "</p>");
document.write("<p>Superscript: " + txt.sup() + "</p>"); getMonth()Returns the month (from 0-11)
document.write("<p>Link: " + getSeconds()Returns the seconds (from 0-59)
txt.link("http://www.w3schools.com") + "</p>"); getTime() Returns the number of milliseconds since midnight Jan 1,
document.write("<p>Blink: " + txt.blink() + " (does not work in IE, 1970
Chrome,
29
or Safari)</p>");
Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 30 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
31 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 32 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
Example 1:
var parents = ["Jani", "Tove"];
Array Object var children = ["Cecilie", "Lone"];
The Array object is used to store multiple values in a single variable. var family = parents.concat(children);
var myCars=new Array(); // regular array (add an optional integer
document.write(family); // Jani,Tove,Cecilie,Lone
myCars[0]="Saab"; // argument to control array's size)
myCars[1]="Volvo"; Example 2:
myCars[2]="BMW"; var brothers = ["Stale", "Kai Jim", "Borge"];
var myCars=["Saab","Volvo","BMW"]; //literal array var family = parents.concat(brothers, children);
var myCars=new Array("Saab","Volvo","BMW"); // condensed array
document.write(family); //Jani,Tove,Stale,Kai Jim,Borge,Cecilie,Lone
var a=new Array(10);
Example 3:
for(var i=0;i<10;i++)
var fruits = ["Banana", "Orange", "Apple" ];
{
document.write(fruits.join() + "<br >"); //Banana,Orange,Apple
a[i]=i+1;
document.write(a[i]); document.write(fruits.join("+") + "<br >"); //Banana+Orange+Apple
} document.write(fruits.join(" and ")); //Banana and Orange and Apple
33 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 34 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
Example 5:
Example 4:
Var fruits= [“Lemon”, “Apple”, “Orange”, “Banana”];
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//Display from index 0 to index 2 (0 and1)
document.write(fruits.pop() ); //Mango //remove the last item
document.write(fruits.slice(0,2)); // Lemon, Apple
document.write(fruits );//Banana,Orange,Apple
document.write(fruits.slice(1));//Apple, Orange, Banana //From 1st
document.write(fruits.push("Lemon","Pineapple") ); //5
document.write(fruits.slice(-2));// Orange, Banana //Last 2 items
document.write(fruits);//Banana,Orange,Apple, Lemon,Pineapple
Example 6:
document.write(fruits.reverse());//Pineapple, Lemon, Apple, Orange,
var fruits = ["Banana", "Orange", "Apple", "Mango"];
Banana
document.write(fruits.sort()); // Apple,Banana,Mango,Orange
document.write(fruits.shift() ); // Pineapple
var n = ["10", "5", "40", "25", "100", "1"];
document.write(fruits);// Lemon, Apple, Orange, Banana
document.write(n.sort()); //1,10,100,25,40,5
document.write(fruits.unshift("Kiwi","Pineapple") ); //6
Example 7:
// added 5th and 6th item
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits); // Kiwi,Pineapple,Lemon,Apple,Orange,Banana
document.write(fruits.toString()); //Banana,Orange,Apple,Mango
35 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 36 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
37 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE 38 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE
For...In Statement
The code in the body of the for...in loop is executed once for each
property.
for (variable in object)
{
code to be executed
}
var person={fname:"John",lname:"Doe",age:25}; //object creation
var x;
for (x in person)
{
document.write(person[x] + " "); // John Doe 25
}
39 Dr. MAREESWARI V, AP(Sr), SITE, VIT,VELLORE