Workshop on Web Programming Day 03
Workshop on Web Programming Day 03
DAY3
Program1://placing script within head section
<html>
<head>
<script>
document.write("Hello World");
</script>
</head>
</html>
<html>
<body>
<script>
document.write("Hello World");
</script>
</body>
</html>
document.write("Sum\t"+(a+b)+"<br>");
document.write("Difference\t"+(a-b)+"<br>");
document.write("Product\t"+ (a*b) +"<br>");
document.write("Quotient\t"+ (a/b) +"<br>");
document.write("Remainder\t"+ (a%b) +"<br>");
1
</script>
</body>
</html>
Program 5: Assignment operators
<html>
<body>
<script>
var a=15,b;
b=a;
document.write("a="+a);
document.write("<br>b="+b);
a+=5;
b*=3;
document.write("<br>+="+a);
document.write("<br>+="+b);</script>
</body>
</html>
</script>
</body>
</html>
Program 8: Find a number is odd/even with prompt window
<html>
2
<body>
<script>
var a=prompt("enter a number");
if(a%2==0)
alert(a+" is an even number");
else
alert(a+" is an odd number");
</script>
</body>
</html>
case 3:
alert("Product="+(a*b));
break;
case 4:
3
alert("Quotient="+(a/b));
break;
default:
alert("Enter any number between 1-4");
break;
}
</script>
</body>
</html>
4
document.write(a[i][j]+"\t");
}
document.write("<br>");
}
</script>
</body>
</html>
Program 13: Functions- Simple Example
<html>
<body>
<script>
function display()
{
document.write("Hello World");
}
display();
</script>
</body>
</html>
Program 14: Functions with arguments and return types
<html>
<body>
<script>
function add(a,b)
{
return(a+b);
}
var x=parseInt(prompt("Enter a number"));
var y=parseInt(prompt("Enter a number"));
document.write("sum="+add(x,y));
</script>
</body>
</html>
Program 15:Object Creation –Method 1 – Example 1
<html>
<body>
<script>
var Employee={name:"Raj",No:123, salary:50000};
document.write("<b>"+"Details of Employee"+"</b>"+"<br>");
Day 03: Java Script
5
Program 16: Object Creation –Method 1- Example 2
<html>
<body>
<script>
function display()
{
document.write("<b>"+"Details of Employee"+"</b>"+"<br>");
document.write("Name: "+Employee.name + "<br>"+"No: "+Employee.No+"<br>"+"Salary:
"+Employee["salary"]);
}
var Employee={name:"Raja",No:123, salary:50000,view:display};
Employee.view();
</script>
</body>
</html>
Program 17: Object Creation –Method 2
<html>
<body>
<script>
var person = new Object();
person.name = "John";
person.age = 50;
person.sex = "Male";
document.write("Name: "+person.name + "<br>"+"Age: "+person.age+"<br>"+"Sex:
"+person["sex"]);
</script>
</body>
</html>
Program 18: Object Creation –Method3
<html>
<body>
<script>
function Person(name, age, sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
var p=new Person("Banu",30,"Female");
Day 03: Java Script
6
<script>
function transpose(a,b)
{
var t=a;
a=b;
b=t;
document.write("<br>Inside Function<br>")
document.write("<br>a="+a);
document.write("<br>b="+b);
}
var x=5,y=7;
document.write("<br>Before Function Call<br>")
document.write("<br>x="+x);
document.write("<br>y="+y);
transpose(x,y)
document.write("<br>After Function Call<br>")
document.write("<br>x="+x);
document.write("<br>y="+y);
</script>
Program 20: Pass by Reference
<script>
function display()
{
document.write("<br><b>"+"Details of Employee"+"</b>"+"<br>");
document.write("Name: "+Employee.name + "<br>"+"No: "+Employee.No+"<br>"+"Salary:
"+Employee["salary"]);
}
function modify(x)
{
x.name="Rani";
}
var Employee={name:"Raja",No:123, salary:50000,view:display};
document.write("<br> Before Function call");
Employee.view();
modify(Employee);
document.write("<br> After Function call");
Employee.view();
</script>
Day 03: Java Script
7
get Name(){return this.name;},
set Number(a){this.No=a;}
};
document.write("<b>"+"Details of Employee"+"</b>"+"<br>");
document.write("Name: "+Employee.Name + "<br>"+"No: "+Employee.No+"<br>"+"Salary:
"+Employee["salary"]);
Employee.Number=567;
document.write("<b>"+"Details of Employee"+"</b>"+"<br>");
document.write("Name: "+Employee.Name + "<br>"+"No: "+Employee.No+"<br>"+"Salary:
"+Employee["salary"]);
</script>
</body>
</html>
Program 22: Classes with constructors,method,getter method,static method
<html>
<body>
<script>
class Employee
{
constructor(name,No,salary)
{
this.name=name;
this.No=No;
this.salary=salary;
}
display()
{
document.write("<b>"+"Details of Employee"+"</b>"+"<br>");
document.write("Name: "+this.Name + "<br>"+"No: "+this.No+"<br>"+"Salary:
"+this["salary"]);
}
get Name()
{
return this.name;
}
static view()
{
document.write("<br>"+"<b>"+"This is a static method"+"</b>"+"<br>");
}
Day 03: Java Script
}
var e1=new Employee("John",1234,80000);
e1.display();
Employee.view();
</script>
8
</body>
</html>
Program 23: Inheritance
<html>
<body>
<script>
class Person
{
constructor(name)
{
this.name=name;
}
}
class Employee extends Person
{
constructor(name,No,salary)
{
super(name);
this.No=No;
this.salary=salary;
}
display()
{
document.write("<b>"+"Details of Employee"+"</b>"+"<br>");
document.write("Name: "+this.name + "<br>"+"No: "+this.No+"<br>"+"Salary:
"+this["salary"]);
}
}
var e1=new Employee("Ramu",112,60000);
e1.display();
</script>
</body>
</html>
Program 24: Array Object methods
<html>
<body>
<script>
var a = new Array("Banana", "Orange", "Apple", "Mango");
for(var i in a)
Day 03: Java Script
document.write(a[i]+"\t");
document.write("<br>"+"Result of toString()= "+a.toString());
document.write("<br>"+"Result of pop() = "+a.pop()+"<br>");
a.push("Guava");
document.write("After Push:");
9
for(var i in a)
document.write(a[i]+"\t");
a.sort();
document.write("<br>After Sort:");
for(var i in a)
document.write(a[i]+"\t");
a.splice(0,1);//removes first element
document.write("<br>After Splice delete:");
for(var i in a)
document.write(a[i]+"\t");
a.splice(2, 0, "Lemon", "Kiwi");
document.write("<br>After Splice insert:"); //starts insertion from 2,removes 0 elements
for(var i in a)
document.write(a[i]+"\t");
a.sort();
document.write("<br>After Sorting:");
for(var i in a)
document.write(a[i]+"\t");
</script>
</body>
</html>
Program 25: String methods
<html>
<body>
<script>
var a = new String("Hello World!");
document.write("<br>"+a);
document.write("<br>"+a.toUpperCase());
document.write("<br>"+a.toLowerCase());
document.write("<br>"+a.charAt(0)); document.write("<br>"+a.replace("World","Everyone"));
document.write("<br>"+a.search("World"));
document.write("<br>"+a.slice(0,5));
document.write("<br>"+a.slice(-10,-5));
document.write("<br>"+a.substr(0,5));
</script>
</body>
</html>
Day 03: Java Script
10
document.write("<br>"+a);
document.write("<br>Year= "+a.getFullYear());
document.write("<br>Month= "+a.getMonth());
document.write("<br>Day= "+a.getDate());
document.write("<br>Hours= "+a.getHours());
document.write("<br>Minutes= "+a.getMinutes());
document.write("<br>Seconds= "+a.getSeconds());
a.setYear(2021);
document.write("<br>"+a);
</script>
</body>
</html>
Program 27: Usage of Regular expression
<html>
<body>
<script>
var a="Hello! How are You?";
var x=a.search(/H/i); //returns posiion of first word starting with H
document.write(x);
var y=a.search(/\d/i);//search for a digit//not present returns -1
document.write(y);
a.replace(/H/i,"Hai")
document.write("<br>After replace:"+a);
</script>
</body>
</html>
Program 28: Events-Inline Handlers
<html>
<body>
<button onclick="alert('Hello')"> Click</button>
</body>
</html>
Program 29: Events-Using Functions
<html>
<body>
<script>
function display()
{
alert('Hello world!');
Day 03: Java Script
}
</script>
<button onclick="display()"> Click</button>
</body>
</html>
11
Program 30: Arithmetic Operations using Event Handling
<html>
<body>
<script>
function calculate()
{
var a=parseInt(document.getElementById("t1").value);
var b=parseInt(document.getElementById("t2").value);
document.write("sum="+(a+b));
document.write("<br>Difference="+(a-b));
document.write("<br>Product="+(a*b));
document.write("<br>Quotient="+(a/b));
}
</script>
Enter First Number <input type="text" id="t1"> <br>
Enter Second Number <input type="text" id="t2"> <br>
<button onclick="calculate()">ADD</button>
</body>
</html>
12