Easy password for site

Updated: 02/01/2021 by Computer Hope

Using the password script below results in a password field popping up when accessing the page where the code is added. Anyone accessing the page must enter the correct password to access the protected page or content.

Example

View the example page to see how the script functions. This script encounters issues if placed on the same page as the destination page (such as we have done on this page). When implementing, we recommend you place this code on a page of its own and direct users that enter the correct password to another page. It is also best to add this code in the HTML <head> tag.

Note

This suggestion is not a recommended method for sites that need to be 100% secure, as a more experienced user can easily bypass this method of password protecting a web page.

Tip

If you are looking for a more secure method of implementing passwords, you must create a .htaccess file on the server containing the security information. If supported, additional information on how to do this is available through your ISP or web host.

Source code

<script type="text/javascript">
var password = "please";
var x = prompt("Enter in the password "," ");
if (x.toLowerCase() == password) {
 alert("Come right in \n \n You've entered in the right password");
 window.location = "good.htm";
}
else {
 window.location = "bad.htm";
}
</script>