Introduction to JavaScript
Javascript allows us to add programming code to our web pages.
It can be used to create small applications such as calculators, games etc.
Javascript has many applications which can enhance the performance of your website. You can
- Detect browsers. Yes, we can detect the browsers from which a
client over the world wide web is making a request and depending on
whether it is Netscape or Microsoft Internet Explorer, we can
show the page better.
- It can be used for "cookies". When a user comes to a specific website, we
can store information about the user's machine, so that when he or she returns
to the same site, we can use the information which was stored for that
specific user.
- We can validate forms using javascript. For instance, we can verify
that that a person has entered the correct values as required by certain
fields; if a user doesn't enter the correct information,
we could prompt the user to do so.
Implementing JavaScript in HTML
Javascript is different from HTML. As a result we need to indicate to the
browser that there exists a javascript embedded in the web page. We do this
using the <script>
tag.
The browser looks for the <script language="javascript">
......
javascript statements....</script>
tags to determine where a
javascripts starts and where it ends. To print something like
"This is an alert message" we write a small javascript as
follows:
<html>
<head>
<title>Javascript Labs</title>
</head><br>
<body>
<script language="javascript">
alert ("This is an alert message.");
</script>
</body>
</html>
|
Check it out
"alert" used in the script above is a standard javascript command that when embedded in a script causes an alert box to pop up; it requires that the user click on the "OK" button to move on.
Entering the alert command outside the script tags causes the browser to interpret it as plain text, and it is just displayed on the screen.
Javascript can be embedded in the <head>
as well as the <body>
of the HTML page. However it is advisable to use it in the <head>
of the web page.
JavaScript Syntax
As well as embedding a javascript is in the <script language="javascript">
and </script>
tags, there are a few other things that you need to note about the syntax of javascript.
- All lines in javascript end in a semicolon.
- Text should always be included within "". If we don't include it in quotes
then it is interpreted to be a javascript variable, and can lead to errors.
- Javascript is case-sensitive for variables, but not for keywords; the
variables a, and A are considered different. But typing
Alert("This is an
error.")
is the same as aLerT("This is an error")
.
We can also use javascript to write the same information on a web page, using
the document.write("string to be printed")
command.
Try running the following portion of javascript:
<html>
<head>
<title>Javascript Labs</title>
</head>
<body>
Sample program
<p>
<script>
document.write("Running from within the script");
</script>
<p>
Out of the script
</body>
</html>
|
Check it out