Changes

Jump to: navigation, search

A Simple JavaScript Example

995 bytes added, 16:00, 9 April 2007
no edit summary
<title>My First JavaScript Example
</head>
 
<body>
 
<script type="text/javascript" language="JavaScript">
document.writeln( "Hello World" );
</script>
 
</body>
</pre>
 
Load the HTML file into JavaScript enabled browser and a web page will be displayed shwoing the text "Hello World". All that this script does is define the standard HTML tags that you would expect to see in an HTML page and also defined embedded JavaScript code that writes text out to the current document (''document'' is the JavaScript object for a web page).
 
Previously we discussed the event driven nature of JavaScript. In the next example we will extend the "Hello World" example to include event handling, in this case designing our script to display an alert dialog when a button is pressed.
 
Edit the previous HTNL file and modify the script code so that is resembles the following:
 
<pre>
<html>
<head>
<title>My First JavaScript Example
</head>
 
<body>
 
<script type="text/javascript" language="JavaScript">
document.writeln( "Hello World" );
</script>
<p>
<input type=button
value="Press me"
onClick="alert('Goodbye cruel world')">
</p>
</body>
</html>
</pre>

Navigation menu