Share this page 

Use a JAVASCRIPT username/password instead the browser's oneTag(s): Varia


URL can be constructed with a special syntax to include an username and password. The format is username:password@www.server.com. The URL is protected using the mechanism included with the web server.
<HTML><HEAD>
<SCRIPT>
 var targetUrl="www.server.com/protect/default.html";
 function login() {
    if (validLogin()) {
       password = document.userInfos.username.value;
       username = document.userInfos.password.value;
       self.location.href=
         "http://"+username+":"+password+"@"+targetUrl;
       }
    }
 function validLogin() {
    if (isBlank(document.userInfos.username.value)){
       alert("Can't be blank");
       document.userInfos.username.focus();
       return false;
       }
    if (isBlank(document.userInfos.password.value)){
       alert("Can't be blank");
       document.userInfos.password.focus();
       return false;
       }
    return true;      
    }
 function isBlank(s) {
    return (s == "");
    }
 </SCRIPT>

 <BODY onLoad="document.userInfos.username.focus();">
 <H1><CENTER>Identification </H1></CENTER>
 <CENTER><TABLE BORDER=1>
 <FORM  NAME=userInfos>
 <TR><TD>User: </TD><TD>
 <INPUT TYPE="text" NAME=username LENGTH=20></TD></TR>
 <TR><TD>Password: </TD><TD> 
 <INPUT TYPE="password" NAME=password LENGTH=20>
 </TD></TR>
 <TR ALIGN=center>
 <TD></TD><TD>
 <INPUT TYPE="button" VALUE="Ok" onClick="login()">
 </TD></TR></TABLE></FORM></CENTER></BODY></HTML>