Simple Password Protection
Quickly lock a page. This script runs immediately when the page loads, asking for a password. If the user enters the wrong one, they are redirected to Google (or any URL you choose).
Password: secret
Copy the Script
<script>
var password = "secret";
var entry = prompt("Enter password to view this page:");
if (entry != password) {
alert("Wrong password!");
window.location = "https://www.google.com"; // Redirect away
}
</script>
Frequently Asked Questions
It is stored directly in the JavaScript variable in the source code. This means it is NOT secure against hackers, but works for keeping out casual visitors.
Yes. You can modify the if-statement to check multiple values (e.g., if (pass == 'A' || pass == 'B')).
No. This is purely client-side code and does not require PHP databases or server setups.