Our complete < script > block for our password with the new if statement looks like this:
<script> function checkPassword() { var password = document.getElementById("passwordBox"); var passwordText = password.value; if(passwordText == "GabbaGabbaHey") { return true; } alert("Access denied! Incorrect password!"); return false; } </script>
Finally we have to make our JavaScript work with our HTML elements. We need to look at the code in the < body > of our page now.
We want to call the checkPassword function when someone clicks on the hyperlink in our page.
So we need to add an onclick attribute to our opening < a > tag, like this:
<body> <p style="font-size: 30pt;">Hello World!</p> <p>Please enter the password to view this website.</p> <p>Password:<input id="passwordBox" type="password"/></p> <a href="secretinfo.html" onclick="return checkPassword();"> Click here to submit password and view website </a> </body>