Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You are correct, the entire program waits for the user to respond, so code that could programatically 'answer' the prompt wouldn't run anyway.

No, you can't answer prompt() from JS code.

In general (unmodified desktop browsers only), I guess not (see other answers). However, if you were looking at other applications of JavaScript alerts, this may be possible. For instance, with the Android SDK, you would use the onJsAlert method from WebChromeClient:

public boolean onJsAlert(WebView view, String url, String message,
    final android.webkit.JsResult result) {
        builder.setTitle(ActivityTitle)
            .setMessage(message)
            .setPositiveButton("OK",
                new AlertDialog.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                        int which) {
                        result.confirm();
                    }
                }).setCancelable(false).create().show();
    return true;
}

It may be worthwhile to inspect the source code to see how this is accomplished. This may have hooks into the browser, but if you really want to get into it, this may be interesting.

Alternatively

If you need to be able to have a sort of alert-style box which can be controlled using JavaScript, then it should also conform to ARIA specifications, as mentioned in the MDN on the alert role. Doing this would allow similar functionality, allow the alert to be styled, and would also be usable by people with accessibility devices.

One down side I can see to using this is that it wouldn't hook into something like what's talked about above. If an Android developer has chosen to override the alerts, then I don't think it would work as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top