Skip Navigation | Groups

This site uses cookies- for more information see the Cookie Policy | Continue

 

Simple Javascript Logon

Single password for all users


This is a simple script that only requires a username (or password) to open a page. However, if the username is not correct, a 404 error page will be displayed.

The username used for this example is: Arthur
Note that the username is CaSe sensitive on Unix servers, but not on Windows servers.

An alternative, more complex, method, with no 404 error pages, can be seen here.

The JavaScript

The JavaScript used can be placed anywhere in the page before the form, in code view, the best place is immediately before the </head> tag.

<script type="text/javascript">
function openSesame() {
var dv = document.logon.userid.value; //This is the value entered into the form textbox.
if (dv != "") { //if nothing was entered ignore these lines, otherwise remove leading and trailing spaces
while (dv.substr(dv.length-1,1)==" ") { //while trailing spaces exist
dv = dv.substr(0,dv.length-1); //remove trailing spaces
}
while (dv.substr(0,1)==" ") { //while leading spaces exist
dv = dv.substr(1,dv.length-1); //remove leading spaces
}
if (dv != "") { // removing the spaces may have left nothing at all, 
// if any characters are left try to open the page
// add the path to the page, and .html to the end to form the URL for the page
// in this case the URL is /fp/articles/jspass/pages/whatever.htmlwhere "whatever" is the contents of the form field.
document.location.href = "/fp/articles/jspass/pages/" + dv + ".html";
return; 
//The script will attempt to move the browser to the protected page - 
//if not successful a 404 error results.
}
}
alert("Need a Password"); //If the field was empty, or all spaces, then demand a password.
}
</script>

To change the password the page the script links to will have to be renamed. For example, renaming the page Arthur.html for this exercise to Benjy.html would change the password to Benjy. However, if the page were placed in a different folder, or the .html changed to, say, .asp, then the script will have to be changed to account for those changes. document.location.href = "/fp/articles/jspass/pages/" + dv + ".html"  would be changed to document.location.href = "newfolder/" + dv + ".asp"

The Form

The code for the login form is:

<form name="logon" onsubmit="openSesame();return false;">
<p><label for="userid">UserId</label>&nbsp;
<input type="text" name="userid" id="userid" size="20" />
<input type="submit" value="Go" name="B1" /></p>
</form>