9

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.

6
  • 2
    If it can be answered from code, why would you use prompt()? Commented Mar 26, 2014 at 21:27
  • 2
    @Alex: I think the OP wants to verify that it is not possible to tamper with prompt programmatically. Commented Mar 26, 2014 at 21:28
  • 2
    A new CAPTCHA method? Commented Mar 26, 2014 at 21:29
  • While it seems to be possible to execute code in the console while a prompt is open, there is no function which returns a reference to the prompt which would allow anyone to interact with it. Commented Mar 26, 2014 at 21:29
  • Correct, I just want to verify there isn't some method of Javascript that would allow a prompt to be answered. Long story short... it's for a synthetic monitoring test. I just want to make sure that when I tell my customer "we can't because your site uses prompt dialogs", I'm not missing something :D Commented Mar 26, 2014 at 21:30

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I totally agree with the intrusiveness of alert() and prompt() dialogs. I've never been a fan. This happens to be a customer's site that I'm creating a synthetic monitoring test for... and of course they use these types of dialogs. And as mentioned above, I just wanted to make sure that when I said "we can't script against those" that I was correct in my assumption :)
@Charlie74 read my update. You can hack it easily by preventing the alert/prompt form ever happening.
Interesting... I'll have to look at that as a possibility. Thanks for the hack... might be something I can use to get around the customer's use of dialog boxes.
2

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

Comments

2

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.