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]
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
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
.
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?");
You can delete window.prompt;
(or set window.prompt = null;
), though this may not work in all browsers.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With