For the passed few weeks I have been explaining how to write your
own Javascript code. A question I have received in the passed is
"How can I password protect my site?" There are several methods
that can be used to do this. One of the simplest is to use a JavaScript.
For those of you that are not familiar with it, JavaScript is a Run
Time scripting language specifically designed for use in Web
Pages. That basically means that you don't have to compile, link
and embed it, processes which can be very complicated for none
programmers. JavaScript's real advantage for most Web
Designers is that it can be quickly learned and easily used.
What is the difference between JavaScript and Java?
They are actually quite different. Java is an Object Oriented
programming language that is written, compiled and executed
independently of a Web Page. To use a Java applet (which
means a "little application"), it must be embedded
<EMBED> into the web page as an Object with the
necessary parameters or variables passed to it. The Java Applet
will then execute independently of the Browser as a separate
program.
JavaScript is an integral part of the Browser. It can be added in
either the HEAD or BODY of a web page. Where you place it
depends on where it will be most appropriate. In fact, you can
add as many separate JavaScripts as you like. Since JavaScript is
executed by the Browser, its functions and variables are global; a
concept that will give many Object Oriented programmers the
shivers. That means that a variable or function in one JavaScript
can be used in another and can access the underlying HTML
DOM (Document Object Model) directly.
If that sounds complex, in fact it is not. It is probably more
intuitive for most novice users, which is exactly the intent. But if
you have little or no programming experience and really don't
want any, then you are in luck. There are many existing
JavaScripts available that can be easily copied and pasted into
your web pages. All you need is a little understanding of how to
do it.
JavaScript is added to a web page with the Script tags. The
correct format is:
Where "…java script code…" is obviously replaced with the
necessary code.
When the Browser sees the <SCRIPT> tag, it stops
interpreting HTML and starts interpreting scripting code. It
defaults to JavaScript so that the "language=JavaScript"
statement is not really necessary. There are other scripting
languages, such as VBScript, so it should be included. The
browser will then interpret and execute the JavaScript until it
reaches the </SCRIPT> tag where it will start executing
HTML again.
The //<!-- and //--> are comment tags that are included for
the sake of older browsers that do not support JavaScript. They
are probably no longer needed but are still usually added.
The important thing to remember is that JavaScript statements
and code must be placed inside the <SCRIPT> tags and
HTML tags must be placed outside. A common mistake by a
novice is to put HTML tags inside the SCRIPT.
So what can you do with JavaScript that you cannot do with
HTML?
A lot. As I said at the outset, this topic resulted from a question
about adding a password to a Web Page. The JavaScript code to
do that is:
In this example, the actual password is "coffee". Change it to
whatever you would like. If the user enters an incorrect
response, they will be directed to the Web Page "incorrect.htm".
Again you should change this to an actual web page of your own
choosing.
If you want to give it a try, simply copy and paste the code into
the HEAD of your HTML page.
There are many other free JavaScripts available. Check out these
sites:
"IMS Web Tips" is a weekly news letter for all web site managers regardless of experience who are looking for detailed information on creating, maintaining and promoting their web sites.
If you like the contents of this newsletter, please recommend it to a friend. Not only will you help us to continue to provide you with useful and informative articles, you could also win $10,000. Click here for details.