(Java Script)Fundamental Java Script

Fundamental merupakan awal dari segala awal yang akan kita pelajari, tapi kalau kita mau mempelajari java script kita sebaiknya belajar sintak HTML, karena unutk php,java script,jquery dan lain lain itu merupakan isi dari segala informasi yang akan kita buat, karena HTML merupakan tampilan utama untuk membuat suatu web atau app web, dipostingan kali ini kita akan membahas basic java script.

Variabel



<html>
<body>

<h1>My First Web Page</h1>

<p id="demo"></p>

<script type="text/javascript">
x=5+5;
document.write(x+" ");

x="5"+"5";
document.write(x+" ");

x=5+"5";
document.write(x+" ");

x="5"+5;
document.write(x+" ");
</script>

</body>
</html>

Operator

x=5+5;
document.write(x);

x=”5″+”5″;
document.write(x);

x=5+”5″;
document.write(x);

x=”5″+5;
document.write(x);

Percabangan if-else

<html>
<head> 
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10

var d=new Date();
var time=d.getHours();

if (time<10)
  {
 	 document.write("<b>Selamat pagi</b>");
  }
else if(time<12){
 	 document.write("<b>selamat siang</b>")
}
else{
	document.write("<b>selamat malam</b>")	
}
</script>
</head>
</html>

Percabangan Switch

<html>
<head>Belajar Java Scrip</head>
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.

var d=new Date();
var theDay=d.getDay();
switch (theDay)
{
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm looking forward to this weekend!");
}

</script>
</html>

Alert

<html>
<head>
<script type=”text/javascript”>
function show_alert()
{
alert(“I am an alert box!”);
}
</script>
</head>
<body>

<input type=”button” onclick=”show_alert()” value=”Show alert box” />

</body>
</html>

Function

<html>
<head>

<script type=”text/javascript”>
function message(){
alert(“contoh penggunaan pesan , pesan ini akan muncul ketika Onload Body”)
}
</script>
<body onload=”message()”>

</body>

</head>
</html>

Perulangan For

<html>
<head>Belajar Java Scrip <br></head>
<script type=”text/javascript”>
var i=0;
for (i=0;i<=5;i++){
document.write(“The number is”+i);
document.write(“<br/>”);
}

</script>
</html>

Perulangan While

<html>
<body>
<script type=”text/javascript”>
var i=0;
while (i<=5)
{
document.write(“The number is ” + i);
document.write(“<br />”);
i++;
}
</script>
</body>
</html>

Break

<html>
<body>
<script type=”text/javascript”>
var i=0;
for (i=0;i<=10;i++){
if (i==3){
break;

}
document.write(“The number is “+i);
document.write(“<br/>”)
}
</script>
</body>
</html>

Try-Catch

<html>
<script type=”text/javascript”>
var txt=””;
function message(){
try{
addlert(“Welcome Guest”);
}catch(err){
txt=”Ini adalah text catch\n\n”;
txt+=”error text”+err.description+”n\n”;
txt+=”Click OK to continoue.\n\n”;
alert(txt);
}
}
</script>
<body>
<input type=”button” value=”View Message” onclick=”message()”/>
</body>
</html>

Form

<html>
<head>
<script type=”text/javascript”>
function confirm_disp(){
var name = confirm(“press a button”)

if(name == true){
document.write(“you pressed the OK button !!!”)
}
else {
document.write(“you pressed tje Cancel Button”)
}
}
</script>
</head>

<body>
<form>
<input type=”button” onclick=”confirm_disp()” value=”display a conform box”>

</form>
</body>
</html>

Referensi :
w3schools.com

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s