|
Contoh 1
Menulis teks dengan Javascript
document.write("This is my first JavaScript!");
|
Hasil di browser
|
This is my first JavaScript!
|
Contoh 2
Menulis tag HTML dengan Javascript
document.write("
Hello World!
");
|
Hasil di browser
Contoh 3
Javasript di body section
document.write("This message is written by JavaScript");
|
Hasil di browser
|
This message is written by JavaScript
|
Contoh 4
Javasript di head section
function message() { alert("This alert box was called with the onload event"); }
|
Hasil di browser
Contoh 5
Javascript eksternal
The actual script is in an external script file called "xxx.js".
|
Hasil di browser
|
This text was written by an external script!
The actual script is in an external script file called "xxx.js".
|
Contoh 6
Javascript statement
document.write("
This is a heading
"); document.write("
This is a paragraph.
"); document.write("
This is another paragraph.
");
|
Hasil di browser
This is a heading
This is a paragraph.
This is another paragraph.
|
Contoh 7
Javascript block
{ document.write("
This is a heading
"); document.write("
This is a paragraph.
"); document.write("
This is another paragraph.
"); }
|
Hasil di browser
This is a heading
This is a paragraph.
This is another paragraph.
|
Contoh 8
Comment satu baris
// Write a heading document.write("
This is a heading
"); // Write two paragraphs: document.write("
This is a paragraph.
"); document.write("
This is another paragraph.
");
|
Hasil di browser
This is a heading
This is a paragraph.
This is another paragraph.
|
Contoh 9
Comment beberapa baris
/* The code below will write one heading and two paragraphs */ document.write("
This is a heading
"); document.write("
This is a paragraph.
"); document.write("
This is another paragraph.
");
|
Hasil di browser
This is a heading
This is a paragraph.
This is another paragraph.
|
Contoh 10
Satu baris comment untuk mencegah eksekusi
//document.write("
This is a heading
"); document.write("
This is a paragraph.
"); document.write("
This is another paragraph.
");
|
Hasil di browser
|
This is a paragraph.
This is another paragraph.
|
Contoh 11
Variabel, nilai dan tampilan
var firstname; firstname="Hege"; document.write(firstname); document.write(" "); firstname="Tove"; document.write(firstname);
The script above declares a variable, assigns a value to it, displays the value, changes the value, and displays the value again.
|
Contoh 12
Pernyataan If
|
var d = new Date(); var time = d.getHours();
if (time < 10) { document.write("Good morning"); }
This example demonstrates the If statement.
If the time on your browser is less than 10, you will get a "Good morning" greeting.
|
Hasil di browser
|
This example demonstrates the If statement.
If the time on your browser is less than 10, you will get a "Good morning" greeting.
|
Contoh 13
Pernyataan If ... Else
var d = new Date(); var time = d.getHours();
if (time < 10) { document.write("Good morning"); } else { document.write("Good day"); }
This example demonstrates the If...Else statement.
If the time on your browser is less than 10, you will get a "Good morning" greeting. Otherwise you will get a "Good day" greeting.
|
Hasil di browser
|
Good day
This example demonstrates the If...Else statement.
If the time on your browser is less than 10, you will get a "Good morning" greeting. Otherwise you will get a "Good day" greeting.
|
Contoh 14
Link acak
Contoh 15
Pernyataan switch
Hasil di browser
|
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.
|
Contoh 16
Alert box
function show_alert() { alert("I am an alert box!"); }
|
Hasil di browser
|