Jumat, 30 April 2010

Minggu ke-9

JavaScript Conditional If ... Else

<html>
<body>

<script type="text/javascript">
var d = new Date();
var time = d.getHours();

if (time < 10)
{
document.write("<b>Selamat Pagi</b>");
}
else
{
document.write("<b>Selamat Siang</b>");
}
</script>

<p>
Ini Contoh IF
</p>

</body>
</html>

Di Browser akan muncul "Selamat Pagi" karena menunjukan pukul 10.


SWITCH CASE

<html>
<body>
<script type="text/javascript">
var d = new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("<b>Finally Friday</b>");
break;
case 6:
document.write("<b>Super Saturday</b>");
break;
case 0:
document.write("<b>Sleepy Sunday</b>");
break;
default:
document.write("<b>I'm really looking forward to this weekend!</b>");
}
</script>

<p>This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>

</body>
</html>


di browser akan muncul:

"I'm really looking forward to this weekend!"

This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.


LOOP

<html>
<body>

<script type="text/javascript">
for (i = 0; i <= 5; i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>

<p>Explanation:</p>

<p>This for loop starts with i=0.</p>

<p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>

<p><b>i</b> will increase by 1 each time the loop runs.</p>

</body>
</html>


HASILNYA:

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

Explanation:

i is equal to 0.

While i is less than , or equal to, 5, the loop will continue to run.

i will increase by 1 each time the loop runs.


ALERT

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("SAYA ALERT BOX!");
}
</script>
</head>
<body>

<input type="button" onclick="show_alert()" value="KLICK BOS" />

</body>
</html>

akan muncul:


0 Komentar:

Posting Komentar

Berlangganan Posting Komentar [Atom]

<< Beranda