Thursday, December 22, 2011

Javascript Example


Ex-1
<html>
<body>
<script type="text/javascript">
var txt = "Hello World!";
document.write("<p>Big: " + txt.big() + "</p>");
document.write("<p>Small: " + txt.small() + "</p>");
document.write("<p>Bold: " + txt.bold() + "</p>");
document.write("<p>Italic: " + txt.italics() + "</p>");
document.write("<p>Fixed: " + txt.fixed() + "</p>");
document.write("<p>Strike: " + txt.strike() + "</p>");
document.write("<p>Fontcolor: " + txt.fontcolor("green") + "</p>");
document.write("<p>Fontsize: " + txt.fontsize(6) + "</p>");
document.write("<p>Subscript: " + txt.sub() + "</p>");
document.write("<p>Superscript: " + txt.sup() + "</p>");
document.write("<p>Blink: " + txt.blink() + " (does not work in IE, Chrome, or Safari)</p>");
</script>
</body>
</html>

Ex-2
<html>
<body>
<script type="text/javascript">

var str="Hello world!";

document.write(str.indexOf("d") + "<br />");

document.write(str.indexOf("WORLD") + "<br />");

document.write(str.indexOf("world"));
</script>
</body>
</html>

Ex-3
<html>
<body>
<script type="text/javascript">

var str="Hello world!";

document.write(str.match("world") + "<br />");

document.write(str.match("World") + "<br />");

document.write(str.match("worlld") + "<br />");

document.write(str.match("world!"));
</script>
</body>
</html>

Ex-4
<html>
<body>
<script type="text/javascript">
var str="Visit Microsoft!";
document.write(str.replace("Microsoft","My Tutorial"));
</script>
</body>
</html> 
Ex-5
<html>
<body>
<script type="text/javascript">
var d=new Date();

document.write(d);
</script>
</body>
</html>

Ex-6
<html>
<body>
<script type="text/javascript">
var d=new Date();

document.write(d.getTime() + " milliseconds since 2022/12/22");
</script>
</body>
</html>

Ex-7
<html>
<body>
<script type="text/javascript">
var d = new Date();
d.setFullYear(2011,22,12);
document.write(d);
</script>
</body>
</html>
Ex-8
<html>
<body>
<script type="text/javascript">
var d=new Date();

document.write("Original form: ");

document.write(d + "<br />");

document.write("To string (universal time): ");

document.write(d.toUTCString());
</script>
</body>
</html>

Ex-9
<html>
<body>
<script type="text/javascript">
var d=new Date();

var weekday=new Array(7);

weekday[0]="Sunday";

weekday[1]="Monday";

weekday[2]="Tuesday";

weekday[3]="Wednesday";

weekday[4]="Thursday";

weekday[5]="Friday";

weekday[6]="Saturday";
document.write("Today is " + weekday[d.getDay()]);
</script>
</body>
</html>

Ex-10

<html>
<head>
<script type="text/javascript">
function startTime()
{

var today=new Date();

var h=today.getHours();

var m=today.getMinutes();

var s=today.getSeconds();

// add a zero in front of numbers<10

m=checkTime(m);

s=checkTime(s);

document.getElementById('txt').innerHTML=h+":"+m+":"+s;

t=setTimeout('startTime()',500);

}
function checkTime(i)

{

if (i<10)

{

i="0" + i;

}

return i;

}

</script>

</head>
<body onload="startTime()">

<div id="txt"></div>
</body>
</html>




No comments:

Post a Comment