Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Javascript prompt() be answered via script?

Basically, I need a sanity check.

Assuming this code:

prompt("Yes or no?")

The user must click an answer here. There isn't a way in Javascript to "answer" the question for the user... correct? Everything I know screams impossible... but wanted to triple check here first.

like image 236
Charlie74 Avatar asked Mar 26 '14 21:03

Charlie74


1 Answers

Nope.

prompt() immediately puts up a modal dialog box, and blocks all further script execution until a value is entered. There is no opportunity to intercept or handle at any point, because no code is allowed to run while it's up.

Perhaps you want a different way to put up a dialog box that's less intrusive like jQuery UI Dialog's


Wait, so you seem more concerned that it could be tampered with and you don't want it to be?

If so, my answer stands as true but there are other ways to get around it. For instance, you can override the prompt() function before it runs to do whatever you want. This snippet would prevent any prompt from appearing and hardcode a specific return value.

window.prompt = function() { return 'spoofed' };
var name = prompt("What's your name?"); // no prompt appears
console.log(name); //=> 'spoofed'

Without knowing what you are trying to accomplish, it's hard to advise how to do it instead. But if you want a prompt that will answer the question "are you a human?", it won't be reliable as it's easily bypassed.

like image 145
Alex Wayne Avatar answered Sep 22 '22 09:09

Alex Wayne