Hello and welcome to my first Tutorial!
Today, I'll be showing how to make a "Website-Login" Prompt.
On something like, for example, "an IP camera".
If you try to preview by browser, It'll prompt you to enter a username and password.
First, the natural way. here are is a way of doing this:
- Go to your favorite HTML editor >
- Create a new file. >
- Name the file "web-login.html" >
- begin coding (steps below)
Make a .js file for JavaScript
on the first JavaScript line, write:
window.onload = function(){
prompt("Title of the box here", "");
}
before the window loads, It'll make a popup box called "Title of the box here",
Replace "Title of the box here", with your title.
Notice: The 2nd line after the title, is blank. You want to keep it blank because that's the text box for the passcode.
The page will keep loading until the box is gone.
in your HTML file, write:
<input type="text" id="your_id" />
let me explain,
The textbox "<input>
" tag you just made, it's very important in our project.
When you type the text in the prompt box, it'll return it's value to the box.
But the box is visible!
Add CSS:
<input style="display: none;" type="text" id="your_id" />
In your JavaScript file, replace the first step "window.onload = function { prompt(...); }
",
with:
var passcode = prompt("Title of the box here", "");
the prompt will still appear before the page loads.
next, write:
document.getElementById("your_id").value = passcode;
It's taking the prompt box value from the user, and giving it to the text box value.
now, we need to test the value of the box:
var passcodeValue = document.getElementById("your_id").value;
Now, we have the value stored in a variable.
Make a testPass()
function to test if the input was changed, automatically:
window.onload = setTimeout(function(){ testPass() }, 200);
function testPass() {
}
Again, in your JavaScript file, we need to make an if
statement:
function testPass() {
if (passwordValue.includes("The_Passcode")) {
// password is correct
}
}
Notice: The_Passcode
, Change that to the true passcode you want. for example: 52719, or 1ceCr3am.
Now you have a password! But what if someone didn't want to use your passcode?
Make an incorrect passcode tester!
It's very simple. We just need an else
statement after the if
.
So, on the same } bracket the if
statement ends on, write an else
:
if (password_value.includes("The_Passcode")) {
// password is correct
} else {
}
in the else
statement, make an alert(), then a page refresh:
else {
alert("Incorrect passcode!"");
// then, refresh the page:
location.replace(window.location.href);
}
you're probably wondering, why do you need to refresh the page?
if you put in the wrong passcode, it'll tell you it's wrong, then, if you put in the right passcode, it'll still tell you it's wrong. You need to reset the page so there are no issues.
just replace location.replace(window.location.href);
to window.location.href = window.location.href;
Viola ~ You have a prompt system.
just use the "replace page" technique:
var page = '<p>Your HTML here.</p>';
function pageContents(NC) {
document.open();
document.write(NC);
document.close();
}
// replace the page
function replacePage() {
pageContents(page);
}
when you trigger the correct passcode, just run the replacePage()
function shown above, and it'll replace the entire page with your HTML contents.
<input style="display: none;" type="text" id="your_id" />
<script>
var passcode = prompt("Title of the box here", "");
document.getElementById("your_id").value = passcode;
var passwordValue = document.getElementById("your_id").value;
window.onload = setTimeout(function(){ testPass() }, 200);
function testPass() {
if (passwordValue.includes("The_Passcode")) {
replacePage();
} else {
alert("Incorrect passcode!");
// then, refresh the page:
location.replace(window.location.href);
}
}
var page = '<p>Your HTML here.</p>';
function pageContents(NC) {
document.open();
document.write(NC);
document.close();
}
// replace the page
function replacePage() {
pageContents(page);
}
</script>