UNIT 3 JS and AJAX (1)
UNIT 3 JS and AJAX (1)
JAVA SCRIPT
<html>
<head>
<script language="javascript" type="text/javascript">
//java script functions
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
//java script functions
</script>
</body>
</html>
Example
<html>
<body>
<script language="javascript">
document.write("Hello World!");
</script>
</body>
</html>
Objects
• Number
• String
• Boolean (true or false value)
• Null (reserved word, means no value)
• Undefined (Variable declared without value will
have the value undefined, no keyword used)
2. Pre- defined objects: They provide properties and
methods to be used by Primitives. Also known as
wrapper objects. Objects contain address at which
value is stored.
2. var a = “Hii”;
document.write(a);
document.write(“<b>” + a + “</b>”);
<script type="text/javascript">
function functionname (parameter-list)
{
Statements
}
</script>
Calling a Function:
<script type="text/javascript">
functionname ();
</script>
• Eg.
<html>
<head>
<script type = "text/javascript">
function a()
{
document.write ("Hello there!");
}
</script>
</head>
<body>
<form>
<input type = "button" onclick = "a()" value = "Say Hello">
</form>
</body>
</html>
• Function Parameters
• The return Statement
JavaScript HTML method
getElementById()
<html>
<body>
<p id="demo" onclick = "a()"> My First Paragraph</p>
<script language = "javascript">
function a()
{
document.getElementById ("demo").innerHTML = 5 + 6;
}
</script>
</body>
</html>
2. document.getElementById ("t").style.color -CSS
Eg.
document.getElementById ("t").style.color = “RED”;
3. document.getElementById ("t").value - To change the element
value.
<html>
<head>
<script language = "javascript">
function sum()
{
a = parseInt(document.getElementById("t").value);
b = parseInt(document.getElementById("t1").value);
c = a+b;
document.getElementById("t2").value = c;
}
</script>
</head>
<body>
A = <input type="text" id= "t"> <br>
B = <input type="text" id= "t1"> <br>
SUM= <input type="text" id= "t2"> <br><br><br>
<input type="Button" value="sum" onclick="sum()">
</body>
</html>
Pop up Boxes
1. Alert Dialog Box:
OK and Cancel
<html>
<body>
<script>
var msg;
if (confirm("Press a button!") == true)
msg = "You pressed OK!";
else
msg = "You pressed Cancel!";
document.write(msg);
</script>
</body>
</html>
3. Prompt Dialog Box:
1.
<html>
<head>
<script language="javascript">
function a()
{
alert("Hello");
}
</script>
</head>
<body>
<input type="Button" value="Click" onclick="a()">
</body>
</html>
TIME INTERVALS
1. setTimeout() – It is used to call another function in given time
period.
setTimeOut(“Function_name()”,Time);
// function to be called, time in milliseconds
function ab()
{
setTimeout(“ab()”,1000);
}
2. setInterval() – It is used to call another function in given time
period.
setInterval(“Function_name()”,Time);
/ function to be called, time in milliseconds
function a()
{
a = setInterval(“ab()”,1000);
}
3. clearInterval() –This function is used to clear interval
of setInterval function & stop its functionality. It is not
associated with setTimeout().
Eg. clearInterval(a);
• Example : Stop Watch
<html>
<head>
<script language="javascript">
var a;
function a()
{
a = setInterval("Start()",1000);
}
sqrt(num)
floor(num)
min(a,b)
max(a,b)
sin(num)
log(num)
2. Boolean Object-
b = new Boolean(False/True/Empty);
eg. b = false;
document.write(b.toString());
3. String Object - String object have many useful string
related functionalities. Some methods are:
Eg.
var s1 = “Hello”;
document.write(s1.concat(“world”));
document.write(s1.charAt(0));
4. Number Object -
• getSeconds()
• getMinutes()
• getHours()
• getDay()
• getMonths()
• getYears()
• getTime()
Eg1.
<script>
var d = new Date();
d.setDate(20);
document.write(d);
</script>
• Eg2. Digital Watch
<html>
<head>
<script language="javascript">
var a;
function a()
d = new Date();
h = d.getHours();
m = d.getMinutes();
s = d.getSeconds();
if(h<10)
h = '0' + h;
if(m<10)
m = '0' + m;
if(s<10)
s = '0' + s;
setTimeout("a()",1000);
</script>
</head>
</body>
</html>
6. Array Object
• Arrays are a special type of objects. Arrays use numbers to access its
"elements".
• Objects use names to access its "members".
Number("") // returns 0
• All modern browsers support the XMLHttpRequest object and have it in-
built.
• The XMLHttpRequest object is used to update parts of a web page,
without reloading the whole page.
Syntax:
Old versions of Internet Explorer (IE5 and IE6) use an ActiveX Object:
<html>
<head>
var x;
function a()
x = new XMLHttpRequest();
x = new ActiveXObject("Microsoft.XMLHTTP");
x.onreadystatechange = function()
document.getElementById("t").innerHTML = x.responseText;
x.send();
</script>
</head>
<body>
<b id="t"></b>
</body>
</html>
AJAX - Send a Request To a Server
• The XMLHttpRequest object is used to exchange data
with a server.
• To send a request to a server, we use the open() and
send() methods of the XMLHttpRequest object:
Method Description
Property Description
Property Description
200: "OK"
Status
404: Page not found