Unlike other programming languages, such as ASP, ASP.NET or PHP, JavaScript is a client-side programming language. JavaScript was first introduced by Netscape as part of their web browser, Netscape Navigator, as a way to give web developers a means of adding interactivity to their web pages.
The difference between client-side scripts and server-side scripts is that client-side scripts contain embedded commands as part of an HTML page and don’t need to be executed on the server in order to run. When a web browser downloads a page the JavaScript commands are loaded in the browser as part of the HTML and they are “triggered” by events on the page, such as a user clicking on a button. This allows the web developer to manipulate page items without having to go back to the server and run the code. Some common uses for JavaScript are form validation, dynamic navigation menus, performing math calculations and opening new windows (pop-ups).
JavaScript commands are typically embedded into an HTML page using the <SCRIPT></SCRIPT> tags. For example, if you wanted to add a button to your web site to close a window the code might look like this:
<HEAD>
<script type="text/javascript">
<!-- Begin
function windowClose() {
alert("You are about to close this window");
if (confirm("Are you sure?")) {
parent.close();
}
else
alert("You have cancelled this action."); {
}
}
// End -->
</script>
</HEAD>
<BODY>
<form>
<input type="button" value="Close Window" onclick=" windowClose()">
</form>
</BODY>
In this sample I’ve created a function called windowClose() that is triggered when the user clicks a button on the page. When the button is clicked a message is displayed telling the user “You are about to close this window”. If they click OK then another box appears asking them to confirm the operation. Clicking Ok again will close the window and clicking cancel will cancel the operation and display the message “You have cancelled this action”.
As you can see, the language itself resembles many other programming languages, such as C, C++, C# or Visual Basic. There are some differences in the way JavaScript is structured.
- JavaScript is case-sensitive. This is an important rule to remember and one of the more common reasons for scripts not executing on a page. In the script above, if I had changed the function on the button from windowClose() to WindowClose() the code would have produced an error.
- JavaScript uses braces {} to group statements into blocks or sections of code.
- A single JavaScript statement can cover multiple lines, or you can have multiple statements on one line, as long as you add a semi-colon to the end of each statement.
JavaScript has a lot to offer web developers. It may not be as flexible as a server-side programming language but provides a quick and simple way to add interactivity to your web site. There are several places on the web where you can get free sample scripts to add to your site. One of my favorites is the JavaScript Source at http://javascript.internet.com/