Hey everyone! I’m working on a small project for my phone. It doesn’t have a built-in lock screen, so I want to make a simple JavaScript app to do the job.
Here’s what I’m thinking:
- When you open the webpage, it asks for a password
- If you type the right password, the page closes
- If you get it wrong, it asks you to try again
I’m pretty new to JavaScript, so I’m not sure where to start. Can anyone help me out with some basic code or point me in the right direction? It doesn’t need to be super fancy - just something that works.
Thanks in advance for any help you can give!
Here’s a simple approach that might work for your needs:
window.onload = function() {
let password = ‘your_secret_password’;
while (true) {
let input = prompt(‘Enter password:’);
if (input === password) {
window.location.href = ‘about:blank’;
break;
}
alert(‘Incorrect. Try again.’);
}
}
Save this as an HTML file and set it as your homepage. It’ll prompt for a password when opened. If correct, it redirects to a blank page. If wrong, it keeps asking. Note that this isn’t highly secure and some mobile browsers might not allow JavaScript to close windows, so we use redirection instead. For better security, consider server-side solutions or dedicated lock screen apps.
I’ve actually implemented something similar for my own phone!
I created a simple HTML file with a script tag. In the JavaScript, I used a while loop that keeps prompting for a password until it’s correct. Something like:
while (true) {
let input = prompt(‘Enter password:’);
if (input === ‘mySecretPassword’) {
window.close();
break;
}
alert(‘Wrong password, try again’);
}
Just replace ‘mySecretPassword’ with your chosen password. Save it as an HTML file and set it as your homepage. It’s not super secure, but it does the job for basic protection.
One caveat—some mobile browsers might not let you close the window with JavaScript. In that case, you could redirect to a blank page instead of closing. Hope this helps!
hey pal, i got a quick fix for ya. try this:
function checkPwd() {
let pwd = ‘secretstuff’;
while (true) {
let guess = prompt(‘whats the password?’);
if (guess === pwd) {
window.location.href = ‘about:blank’;
break;
}
alert(‘nope, try again’);
}
}
window.onload = checkPwd;
just change ‘secretstuff’ to whatever u want. not super secure but gets the job done!