Beruflich Dokumente
Kultur Dokumente
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Example 10
Using a for loop to generate a series of characters
<HTML>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Example 11
Using logical operators
<HTML>
<HEAD>
<SCRIPT LANGUAGE = "JavaScript">
var marks = prompt("Enter percentage marks");
if ((marks <= 80 ) && (marks > 70))
{
document.write("Grade B");
}
var temp = prompt("Enter temperature below", "in degrees centigrade");
if ((temp < 0) || (temp > 100))
{
document.write("<BR>No liquid water.");
}
else
{
document.write("<BR>Liquid water exists.");
}
</SCRIPT>
</HEAD>
</HTML>
Example 12
Displaying alerts
<HTML>
<HEAD>
<SCRIPT LANGUAGE = "JavaScript">
alert("This is an example of an alert!");
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Example 13
Displaying alerts by clicking a hyperlink
<HTML>
<HEAD>
</HEAD>
<BODY>
<A HREF = "#" onClick = "alert('Thank you! Do it again');">
Click on me</A>
</BODY>
</HTML>
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Example 14
Displaying alerts by mouse over
<HTML>
<HEAD>
</HEAD>
<BODY>
<A HREF = "#" onMouseOver = "alert('Thank you! Do it again');">
Move the mouse pointer over me</A>
</BODY>
</HTML>
Example 15
Preventing auto re-loading of a page
<HTML>
<BODY>
1. This page has several lines. This page has several lines.
<br>
2. This page has several lines. This page has several lines.
<br>
3. This page has several lines. This page has several lines.
<br>
4. This page has several lines. This page has several lines.
<br>
5. This page has several lines. This page has several lines.
<br>
6. This page has several lines. This page has several lines.
<br>
7. This page has several lines. This page has several lines.
<br>
8. This page has several lines. This page has several lines.
<br>
9. This page has several lines. This page has several lines.
<br>
10. This page has several lines. This page has several lines. <br>
11. This page has several lines. This page has several lines. <br>
12. This page has several lines. This page has several lines. <br>
13. This page has several lines. This page has several lines. <br>
14. This page has several lines. This page has several lines. <br>
15. This page has several lines. This page has several lines. <br>
16. This page has several lines. This page has several lines. <br>
17. This page has several lines. This page has several lines. <br>
18. This page has several lines. This page has several lines. <br>
19. This page has several lines. This page has several lines. <br>
20. This page has several lines. This page has several lines. <br>
21. This page has several lines. This page has several lines. <br>
22. This page has several lines. This page has several lines. <br>
23. This page has several lines. This page has several lines. <br>
24. This page has several lines. This page has several lines. <br>
25. This page has several lines. This page has several lines. <br>
26. This page has several lines. This page has several lines. <br>
27. This page has several lines. This page has several lines. <br>
28. This page has several lines. This page has several lines. <br>
29. This page has several lines. This page has several lines. <br>
30. This page has several lines. This page has several lines. <br>
<p>
<A HREF = "#" onClick = "alert('Thank you! Do it again'); return false">Click on me(without
reloading)</A>
</BODY>
</HTML>
Example 16
Using buttons to display alerts
<HTML>
<BODY>
<INPUT TYPE = "button" VALUE = "Click to display an alert" onClick = "alert('This is an alert');">
</BODY>
</HTML>
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Core JavaScript
Array
An object representing an array. The following syntax is used to create an Array object :
new Array()
new Array(array-length)
new Array(element0, element1, , elementN)
Properties
length
Methods
join(separator)
pop()
push()
reverse()
shift()
slice(begin,numb)
unshift()
sort(compareFunction)
Example 19
Creating and printing arrays
<SCRIPT LANGUAGE="JavaScript">
var elements = new Array("Wind", "Rain", "Fire");
document.write(elements + "<P>");
var myArray = new Array();
myArray[0] = "disk";
myArray[5] = "CPU";
document.write(myArray + "<P>");
var numbers = new Array(3);
for (i=0; i<5; i++)
numbers[i] = i;
document.write(numbers + "<P>");
var numbers = new Array(10);
for (i=0; i<numbers.length; i++)
numbers[i] = i;
document.write(numbers + "<P>");
</SCRIPT>
Example 20
Manipulating array elements 1
<SCRIPT LANGUAGE="JavaScript">
var myArray = new Array("Wind", "Rain", "Fire");
document.write(myArray.join("+") + "<BR>");
document.write(myArray.join(", ") + "<P>");
document.write("Array before pop: " + myArray + "<BR>");
popped = myArray.pop();
document.write("Array after pop : " + myArray + "<BR>");
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Boolean
An object representing Boolean value. The following syntax is used for creating a Boolean object :
new Boolean(value)
where value is the initial value of the Boolean object. If the value is omitted or 0 or null or false or the
empty string , the object has an initial value of false. All other values create an object with an initial
value of true.
Example 23
Manipulating Boolean objects
<SCRIPT LANGUAGE="JavaScript">
bNoParam = new Boolean();
bZero = new Boolean(0);
bNull = new Boolean(null);
bEmptyString = new Boolean('');
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
of
of
of
of
of
of
of
of
of
Date
An object representing dates and times. The following syntax is used for creating a Date object :
new Date();
new Date(date-string);
new Date(milliseconds);
new Date(yr mo day hr min sec ms);
Here date-string can be : 12 January 2003
Milliseconds in the number of ms since Jan 1, 1970, 00:00:00
Here mo ranges from 0 (January) to 11(December).
JavaScript stores dates internally as numbers representing milliseconds since January 1, 1970 00:00:00.
Dates prior to this are not allowed.
Methods
getDate, setDate
getDay, setDay
getHours, setHours
getMinutes, setMinutes
getMonth, setMonth
getSeconds, setSeconds
getYear, setYear
getTime, setTime
toGMTString
Example 24
Manipulating Date objects
<SCRIPT LANGUAGE="JavaScript">
Xmas2000 = new Date("25
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
document.write("<B>Value
</SCRIPT>
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
Math
An object that has properties and methods for mathematical constants and functions. To use a Math
object, use the following syntax:
Math.propertyname
Math.methodname
Properties
E
LN10
LN2
LOG10E
LOG2E
PI
SQRT1_2
SQRT2
Methods
abs(x)
ceil(x)
sin(x), cos(x), tan(x)
exp(x)
floor(x)
max(x,y)
min(x,y)
pow(x,y)
random()
sqrt(x)
Example 25
Manipulating Math objects 1
<SCRIPT LANGUAGE="JavaScript">
document.write("<b>Value of e: </b>" + Math.E + "<BR>")
document.write("<b>Value of pi: </b>" + Math.PI + "<BR>")
document.write("<b>Value of square root 2: </b>" + Math.SQRT2 + "<BR>")
</SCRIPT>
Example 26
Manipulating Math objects 2
<SCRIPT LANGUAGE="JavaScript">
document.write(Math.abs(-2.5) + "<BR>")
document.write(Math.ceil(-2.5) + "<BR>")
document.write(Math.floor(-2.5) + "<BR>")
document.write(Math.pow(2,5) + "<BR>")
document.write(Math.sin(Math.PI/2) + "<BR>")
</SCRIPT>
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
10
Number
An object representing a number. The following syntax is used for creating a Number object :
new Number(value)
where value is a numerical value.
Properties
MAX_VALUE
MIN_VALUE
POSITIVE_INFINITY
NEGATIVE_INFINITY
NaN
Methods
toString(radix)
Example 27
Manipulating Number objects 1
<SCRIPT LANGUAGE="JavaScript">
var n = new Number(2);
document.write(Number.MAX_VALUE + "<BR>");
document.write(Number.MIN_VALUE + "<BR>");
document.write(n/0 + "<BR>");
document.write(Math.pow(-n,.5) + "<BR>");
</SCRIPT>
Example 28
Manipulating Number objects 2
<SCRIPT LANGUAGE="JavaScript">
var first_num = new Number(5487);
document.write("<b>In Decimal : </b>" + first_num + "<BR>");
document.write("<b>In Octal : </b>" + first_num.toString(8) + "<BR>");
document.write("<b>In Hex : </b>" + first_num.toString(16) + "<BR>");
document.write("<b>In Pental : </b>" + first_num.toString(5) + "<BR>");
document.write("<b>In Binary : </b>" + first_num.toString(2) + "<BR>");
</SCRIPT>
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
11
String
An object representing a string of characters. The following syntax is used for creating a String object :
new String(string)
where string is a string of characters
Properties
length
Methods
big()
bold()
italics()
charAt(index)
Example 29
Manipulating String objects 1
<SCRIPT LANGUAGE="JavaScript">
var worldString=new String("Hello, world");
document.write("<P>" + worldString) ;
document.write("<P>" + worldString.big());
document.write("<P>" + worldString.big().big());
document.write("<P>The string length is " + worldString.length);
</SCRIPT>
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
12
anyString.charAt(0)
anyString.charAt(1)
anyString.charAt(2)
anyString.charAt(3)
anyString.charAt(4)
+
+
+
+
+
"<BR>");
"<BR>");
"<BR>");
"<BR>");
"<P>");
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
13
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
14
Problems
Problem 1
Write a JavaScript code to generate prompt dialog boxes one after one. The first prompt box should ask
the user to specify the first primary color by typing either R or G or B. The second prompt box should
likewise ask the user to specify the second primary color. Based on the user choices the program should
generate the message Your favourite color is x where x is the name of the secondary color formed from
the specified primary colors. The color of the message text should also be same as x.
Problem-2
Create a password protected Web page where the user is repeatedly asked to enter a password as long as
the correct password is not supplied.
Problem 3
Generate a grid of characters by asking the user to specify how wide and high the grid should be.
Problem 4
Write a JavaScript program to generate two prompt dialog boxes to the user. The first one should accept a
numerical value while the second one the name of a unit, either inch or cm. The program should
convert the value to the other unit and display the result in an alert box.
Problem 5
Write a JavaScript program to (a) accept a date from the user and display the name of the weekday (i.e.
Monday, Tuesday ... etc.) it corresponds to (b) Print the current date in the form mm/dd/yyyy.
Problem 6
Write a JavaScript program that will accept two dates and calculate the number of days between them.
Problem 7
Write a JavaScript function to accept a value for x from the user and compute the value of :
sin( x 2 ) + cos(1 / x 2 )
Ranjan Parekh
School of Education Technology, Jadavpur University, Calcutta, India
www.geocities.com/rparekhju/
15