Troncoso
Troncoso

Reputation: 2463

window.alert and confirm work, but not prompt

I'm trying to display a Javascript prompt for the user to enter data (as prompts are used for). But, it won't show. window.alert shows, and so does window.confirm. But, not prompt. Here's the code I'm trying to run:

var is_expired = document.getElementById("is_expired").value;
    if (is_expired == "yes") {
        alert(is_expired);
        var answer = prompt("Are you sure you?");
        alert(answer);
    }

When this runs, the if statement is entered. The first alert displays "yes" as it should. But, then, it skips the prompt line. The next alert displays saying "Undefined".

Now, if the only thing I change is the keyword prompt, it will work, as such:

var is_expired = document.getElementById("is_expired").value;
    if (is_expired == "yes") {
        alert(is_expired);
        var answer = confirm("Are you sure you?");
        alert(answer);
    }

When I run this, the confirm box appears. If I click Okay, then the next alert says "true" and if I click Cancel the alert says "false".

So, the big takeaway here is that there isn't a syntax error that's causing the Javascript to stop. It's actually skipping over that single line and continuing execution. What's wrong with that line? I've checked multiple sites to ensure it's correct. Adding the second "default" parameter does not help either. I tried that. Also, There are no Javascript errors to indicate the problem.

I went here and changed it to my code (I hardcoded is_expired to be yes), and it works there.

Any help would be fantastic. Thanks.

EDIT: To be clear, I'm not relying on W3school's example to be accurate, I used their "try it out" page to test my own code. I also did this on jfiddle and it worked fine. Using the console to check the function returns "undefined".

EDIT2: Actually, scratch that. I accidentally hit enter again when there was no command in the console. The actual output for prompt is:

[12:07:55.940] [object Function]

Upvotes: 4

Views: 12805

Answers (3)

gen_Eric
gen_Eric

Reputation: 227190

As suggested, something in your code has overridden the window.prompt method with function prompt(s) { window.status = s }. Not sure if this was intentional or not.

There are a few methods you can use to "restore" the original prompt.

  1. You can backup the original at the very start of your page:

    var originalPrompt = window.prompt;
    // Whatever code is on your page
    function prompt(s) { window.status = s }
    

    Then use originalPrompt instead:

    var answer = originalPrompt("Are you sure you?");
    
  2. You can delete window.prompt; (or set window.prompt = null;), though this may not work in all browsers.

  3. You can create an iframe (which creates a new window environment), and then "steal" the prompt from there.

    var i = document.createElement('iframe');
    i.style.display = 'none';
    document.body.appendChild(i);
    window.prompt = i.contentWindow.prompt;
    

Upvotes: 2

Christoph
Christoph

Reputation: 51181

prompt should pretty much work in every browser. One possibility is to check if you didn't accidently override this function somewhere in your source. If you type prompt in the browserconsole when on your site, it should state something like this:

> prompt
function prompt() { [native code] }

// or put in your code:
alert(window.promt);

Otherwise it got overridden somehow (most likely by forgetting the var keyword):

> prompt = function(){alert("foo!")}
> prompt
function (){alert("foo")}

// calling prompt() would now pop an alert box

Upvotes: 3

000
000

Reputation: 27227

When declaring variables in a function, make sure you use the var keyword so it doesn't clobber the global namespace. It sounds like you're doing this somewhere:

function my_thing() {
    var i = 0;
    var j = 0;
    prompt = "What the heck";
}

Omitting the var keyword puts prompt in the global namespace, clobbering the default prompt function.

Upvotes: 2

Related Questions