<html>
<head>
<script type=”text/javascript”>
function message()
{
alert(“This alert box was called with the onload event”);
}
function show_alert()
{
alert(“This is an alert box”);
}
function show_confirm()
{
var r=confirm(“Press a button”);
if (r==true)
{
alert(“You pressed OK”);
}
else
{
alert(“You pressed Cancel”);
}
}
function show_prompt()
{
var name=prompt(“Please enter your name”,”Harry Potter”);
if (name!=null && name!=”")
{
document.write(“Hello ” + name + “! How are you today?”);
}
}
function product(a,b)
{
return a*b;
}
</script>
<!– try…catch… statement –>
<script type=”text/javascript”>
var txt=”";
function try_message()
{
try
{
adddlert(“Welcome guest!”);
}
catch(err)
{
txt=”There was an error on this page.\n\n”;
txt+=”Error description: ” + err.description + “\n\n”;
txt+=”Click OK to continue.\n\n”;
alert(txt);
if(!confirm(txt))
{
document.location.href=”http://www.w3schools.com/“;
}
}
}
function throw_catch()
{
var x=prompt(“Enter a number between 0 and 10:”,”");
try
{
if(x>10)
{
throw “Err1″;
}
else if(x<0)
{
throw “Err2″;
}
else if(isNaN(x))
{
throw “Err3″;
}
}
catch(er)
{
if(er==”Err1″)
{
alert(“Error! The value is too high”);
}
if(er==”Err2″)
{
alert(“Error! The value is too low”);
}
if(er==”Err3″)
{
alert(“Error! The value is not a number”);
}
}
}
</script>
</head>
<!– <body onload=”message()”> –>
<body>
<!–
<script type=”text/javascript”>
document.write(“<h1>Hello World</h1>”);
</script>
–>
<script type=”text/javascript”>
/*
The code below will write
one heading and two paragraphs
*/
document.write(“<h1>This is a heading</h1>”); //write a heading
document.write(“<p>This is a paragraph</p>”);
document.write(“<p>This is another paragraph.</p>”);
</script>
<script type=”text/javascript”>
var x=5;
var carname=”Volvo”;
y=5;
//carname=”Volvo”
z=x+y;
document.write(z);
document.write(“<br/>”)
txt1 = “What a very”;
txt2 = “nice day”;
txt = txt1+” “+txt2;
document.write(txt);
//add string to number, result is string
z+=’1′;
z=’<br/>’+z;
document.write(z);
document.write(‘<br/>’+'number==string: ‘);
document.write(1==’1′);
document.write(‘<br/>’+'number===string: ‘);
document.write(1===’1′);
//if statement
document.write(‘<br/>’);
age = 16
if(age<18) document.write(“too yong”)
</script>
<br/>
<script type=”text/javascript”>
//if … else if … else … statement
var d = new Date();
var time=d.getHours();
if (time<10)
document.write(“Current time is “+d+”. ” + “<b>Good morning</b>”);
else if(time>10 && time<16)
document.write(“Current time is “+d+”. ” + “<b>Good day</b>”);
else
document.write(“Current time is “+d+”. ” + “<b>Hello World</b>”);
</script>
<br/>
<script type=”text/javascript”>
//switch statement
document.write(‘<br/>’);
var d = new Date();
theDay = d.getDay();
switch(theDay)
{
case 5: document.write(“Friday”);break;
case 6: document.write(“Saturday”);break;
case 0: document.write(“Sunday”); break;
default: document.write(“I’m looking forward to this weekend!”);
}
</script>
<br/>
<!–alert box–>
<input type=”button” onclick=”show_alert()” value=”Show alert box” />
<button onclick=”show_alert()”>Show alert box</button>
<!–confirm box–>
<input type=”button” onclick=”show_confirm()” value=”Show confirm box” />
<!–Prompt box–>
<input type=”button” onclick=”show_prompt()” value=”Show prompt box” />
<script type=”text/javascript”>
//call a function
document.write(‘<br/>’+”product(3,4)=”+product(3,4));
</script>
<!–for loop–>
<br/>
<script type=”text/javascript”>
var i=0;
for (i=0;i<=10;i++)
{
if (i==5) continue;
if (i==9) break;
document.write(“The number is ” + i);
document.write(“<br/>”);
}
</script>
<br/>
<!–while loop–>
<script type=”text/javascript”>
var i=0;
while (i<=5)
{
document.write(“The number is ” + i);
document.write(“<br/>”);
i++;
}
</script>
<!– for … in … loop –>
<br />
<script type=”text/javascript”>
var x;
var mycars = new Array();
mycars[0]=’Saab’;
mycars[1]=”Volvo”;
mycars[2]=”BMW”;
for (x in mycars)
{
document.write(mycars[x] + “<br/>”);
}
</script>
<br />
<!–onChange–>
<input type=”text” size=”30″ id=”email” onchange=”checkEmail()”>
<!–onSubmit–>
<form method=”post” action=”xxx.htm” onsubmit=”return checkForm()”>
<br />
<a href=”http://www.w3schools.com” onmouseover=”alert(‘an onMouseOver event’);return false”><img src=”http://www.w3schools.com/images/icon_green_sd.gif” alt=”W3Schools” /></a>
<br/>
<input type=”button” value=”View message” onclick=”try_message()” />
<!–Try throw catch –>
<input type=”button” onclick=”throw_catch()” value=”Show try_throw_catch” />
<br/>
<!– special text, use ‘\’ –>
<script type=”text/javascript”>
var txt=”We are the so-called \”Vikings\” from the north.”;
document.write(txt);
document.write (“You \& I are singing!”);
</script>
<script type=”text/javascript”>
var txt=”how are you?”;
document.write(‘<br/>’+”txt.length = ” + txt.length);
document.write(txt.toUpperCase());
document.write(“<br />”);
document.write(txt.indexOf(“a”) + “<br />”);
document.write(txt.indexOf(“ARE”) + “<br />”);
document.write(txt.indexOf(“are”));
document.write(“<br />”);
var str=”Visit Microsoft!”;
document.write(str.replace(“Microsoft”,”W3Schools”));
document.write(“<br />”);
var str=”Hello world!”;
document.write(str.match(“world”) + “<br />”);
document.write(str.match(“World”) + “<br />”);
document.write(“<br />”);
document.write(“<p>Link: ” + txt.link(“http://www.w3schools.com“) + “</p>”);
document.write(“<p><a href=\”http://www.w3schools.com\“>” + txt+ “</a></p>”);
</script>
</body>
</html>
Filed under: Computer, Programming | Leave a Comment »