Rishav Choudhuri Rishav Choudhuri - 25 days ago 7
HTML Question

how to create a website that prompts the user to enter his login id and password

I want to create a website that asks the user to enter his login ID and password. Please help me to do it.

In this instance the login ID is abcd and password is abcd1234.
I am trying this piece of code(JavaScript language).But I don't know how to stop the user from visiting the site or redirect him to another site. How do I do it in a simple way?

<html>
<body>
<script>
function login()
{
var a=prompt("Enter your user ID");
var b=prompt("Enter your password");
if(a=="abcd" && b=="abcd1234")
{
alert("Thank you for visiting");
}
else
{
alert("Wrong ID or password!!");
}
}
login();
</script>
<h1>Hello world!!!Welcome!</h1>
</body>
</html>

Answer

Well, you could redirect them, if they haven't entered the correct credentials, by simply setting the window location to something else.

window.location.href = 'www.google.com'

Or you can just close the window, by calling.

window.close();

Of course, the name and password here are stored client side, so a fairly proficient user can simply read it from the source code. If you want it to be secure you can do it on the backend, but that requires knowledge of a backend language and makes the scope of the question considerably bigger.

Comments