It's not much of a prompt if the user can't see it [Raising awareness of window.prompt's troublesome behavior in Internet Explorer on Windows 8]
For as long as people have been running scripts on web pages, window.prompt has been a simple way to get user input.
Although it rarely offers the ideal user experience, simplicity and ubiquity make window.prompt
a good way ask the user a question and get a simple response.
And as part of the HTML5 specification, you know you can trust window.prompt
with all your cross-browser prompting needs!
Well, unless you're running the metro/immersive/Windows Store/non-desktop version of Internet Explorer on Windows 8.
Then you're in trouble.
You see, window.prompt
has a strange implementation in immersive IE on Windows 8.x: it doesn't actually prompt the user.
Instead, it silently returns undefined
without doing anything!
To make matters worse, this behavior doesn't seem to be documented.
The result is a certain amount of surprise and workarounds by people who rely on window.prompt
behaving as specified.
Unfortunately, this is the kind of thing you don't know is broken until a customer reports a problem. :( But fortunately, once you know about this behavior, you can detect it and take steps to accommodate it. I got burned recently, so I've written this post to raise awareness in the hope that I can save some of you a bit of trouble.
In brief, there are three classes of behavior for window.prompt
in Internet Explorer on Windows 8:
User Action | Return value |
---|---|
Provide a response | string (possibly empty) |
Cancel the prompt | null |
Running immersive IE | undefined (no prompt) |
If this post helps just one person, I'll consider it a success. :)
Aside: While the choice of
undefined
for the immersive IE scenario is a reasonable one (it being a "falsy" value likenull
), I first thought it was at odds with the HTML5 specification. But on closer reading I think this behavior falls outside the specification which only concerns itself with scenarios where the prompt is displayed. So while I'd say immersive IE is violating the spec by not showing the prompt, its use ofundefined
as a return value is sensible and valid. :)Further aside: If you think the chart above is all you need, you may still be in for a surprise. This page claims the Opera browser returns
undefined
when a user cancels the prompt - a violation of (my understanding of) the HTML5 specification.