STUDY MATERIAL CLASSROOM EXERCISES SAMPLES
 
Site Map
Java - Home
Intro 1
Intro 2
Session 1
Assignment
Transitions
Colours
Java Script
Java Applet
Tutorial
Java games
Java games
Java games
Java games
Painting

Color Schemes:

Password script

1. Password

Password is the name of the desired HTML document

2. Scripts and elements to be inserted

a) In the <HEAD> section

<SCRIPT LANGUAGE="JavaScript">
<!-- beginning of script
function checkPswd(ext) {
if ((document.pswdForm.pswd.value == null) || (document.pswdForm.pswd.value == ''))
alert('Mot de passe invalide.');
else this.location.href = document.pswdForm.pswd.value + ext;
}
// end of script -->
</SCRIPT>

b) at the cursor position (body of the document)

<FORM NAME="pswdForm">
<INPUT TYPE="password" NAME="pswd" SIZE=24 MAXLENGTH=40>
<INPUT TYPE="button" VALUE="Ok" onClick="checkPswd('.html')">
</FORM>

3. Description of script

The goal is to restrict access to a document (or documents) by making it necessary to enter the name of the file (HTML page) in order to open the page.

To ensure restriction of access to a given section (HTML page) make sure that there are no other ways to access the site (link, menu, etc.) The level of protection is minimum but the user must know the password which is in fact the filename of the page to be loaded (the filename without the extension is the password).

To encode the password, you use the <INPUT> element. This element is set to display characters as asterisk (*) by setting the TYPE attribute to password: TYPE="password".

The procedure used [checkPswd(ext)] employs a conditional instruction (if) to verify that the contents of the <INPUT> element which is set for a password, is not a null value (document.pswdForm.pswd.value == null) or empty (document.pswdForm.pswd.value == '').

If this is the case, an alert message is displayed: this is achieved with the alert() method of the window object, which we saw earlier: [alert('Invalid password.')].

If the opposite is true (else), the script changes the location property of the Window object in order to load the desired HTML page [this.location.href = document.pswdForm.pswd.value + ext;] by using the value entered in the password field (document.pswdForm.pswd.value) as a URL address of the document, to which it adds (+ operator) the corresponding extension (in this case '.html'). The assistant specified the extension and supplied it as the argument for the checkPswd('.html') procedure.

This is the reason why you are asked to enter the file name without the extension (html, htm...).

Finally, the "onClick" button event triggers the verification function and then loads the page[checkPswd()].

4. How to modify parameters

If you want to load a different kind of document, all you have to do is modify the extension in the <FORM>, element in case the document has a different extension (i.e. ".htm"). To do so you must modify the argument of the [checkPswd('.htm')] procedure of the button's onClick event.

Please keep in mind that the only protection offered with this script is the name of the desired HTML document. Once the user knows the name of the page, he has access to its contents (minimum security). However, nothing prevents you from changing the name of the page and supplying it only to persons authorized to access the page.


 
Scroller 1
Scroller 2
Java Quiz
Drop-down
Clock 1
Clock 2
Eyeballs
Highlighted
Intro-screen
Link Flasher
Password
Mouse Over
Tricks
Weight
Circling Text
Builder
HOME