Professional Documents
Culture Documents
Web Designing
Semester - III
– Text Editor
– Browser
– To run JavaScript we need a compatible
browser like Netscape Navigator 2 .0 and up
• <SCRIPT LANGUAGE="javascript">
document.write("Hello, world")
</SCRIPT>
Output:
• Hello, world
• <script type="text/javascript">
<!-- // This is a single line JavaScript
comment
document.write("I have comments in my
JavaScript code!");
//document.write("You can't see this!"); //-->
</script>
• <head>
• <title>Data Types and Variables</title>
• </head>
• <body>
• <script type="text/javascript">
• x = 1024;
• document.write("<p>x = " + x + "</p>");
• x = “Peter";
• document.write("<p>x = " + x + "</p>");
• </script>
• </body>
• </html>
Copyright @ Amity University
Variables
<script language=“JavaScript”>
Alert(total_passenger);
</script>
• confirm("sometext");
– +, -, /, % (arithmetic)
Syntax
• if (condition)
{
code to be executed if condition is true
}
Copyright @ Amity University
If Statement
• if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not
true
}
Copyright @ Amity University
if-else statement
• switch (myColor)
• {
• case "Blue":
• document.write("Just like the sky!");
• break
• case "Red":
• document.write("Just like shiraz!");
• break
• default:
• document.write("Suit yourself then...");
• }
• //-->
• </script>
• </head>
• </html>
• document.myform.text1.value"><BR>
• To Field <INPUT TYPE="text" NAME="text2">
• </FORM>
• </body>
• </html>
Copyright @ Amity University
Output
• From Field
• To Field
• </script>
• </head>
• <body>
• <form>
• <form>
• <input type="text" name="email_address"
• size="40" value="Please enter your email address"
• onfocus="this.value=''"/>
• </form>
• </body>
• </html>
• </script>
• </head>
• <body onload = "alert('Thanks for visiting my page!')">
• My Page
• </body>
• </html>
• My Page
score[0] = 35
score[1] = 56
score[2] = 10
sum=score[0]+score[1]+score[2];
alert(sum) ;
Copyright @ Amity University
Example
• <html>
• <HEAD>
• <SCRIPT language="JavaScript">
• <!--
• function display_quote()
• {
• var quote= new Array(5)
• quote[0]="I like JavaScript.";
• quote[1]="I used to like Java.";
• quote[2]="JavaScript rules.";
• quote[3]="Help! JavaScript Error!";
• quote[4]="Just Kidding.";
• var x=0;
• for (x=0; x<5; x++)
• {
• alert(quote[x]);
• }
• }
• //-->
• </SCRIPT>
• </HEAD>
• <BODY>
• <input type=button onClick="display_quote()">Click Here, I dare you!
• </BODY>
• </html>
• function functionname(var1,var2,...,varX)
{
some code
}
• The parameters var1, var2, etc. are
variables or values passed into the
function. The { and the } defines the start
and end of the function.
• <html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>
<body>
<input type="button" value="Click me!"
onclick="displaymessage()" />
</body>
</html>