This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 25686 - alert() needs two overloads so alert(undefined) can alert "undefined"
Summary: alert() needs two overloads so alert(undefined) can alert "undefined"
Status: RESOLVED FIXED
Alias: None
Product: WHATWG
Classification: Unclassified
Component: HTML (show other bugs)
Version: unspecified
Hardware: Other other
: P3 normal
Target Milestone: Unsorted
Assignee: Ian 'Hixie' Hickson
QA Contact: contributor
URL: http://www.whatwg.org/specs/web-apps/...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-05-13 17:29 UTC by contributor
Modified: 2014-05-16 16:54 UTC (History)
4 users (show)

See Also:


Attachments

Description contributor 2014-05-13 17:29:36 UTC
Specification: http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html
Multipage: http://www.whatwg.org/C#the-window-object
Complete: http://www.whatwg.org/c#the-window-object
Referrer: http://www.whatwg.org/specs/web-apps/current-work/multipage/

Comment:
alert() needs two overloads so alert(undefined) can alert "undefined"

Posted from: 63.245.221.34 by bzbarsky@mit.edu
User agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0
Comment 1 Boris Zbarsky 2014-05-13 17:34:44 UTC
People are asking for alert(undefined) to show "undefined".

Current behavior in browsers:

Chrome, Firefox 27 and earlier, IE11:
  alert() shows ""
  alert(undefined) shows "undefined"

Firefox 28 and later, current spec: 
  alert() shows ""
  alert(undefined) shows ""

Safari 7: 
  alert() shows "undefined"
  alert(undefined) shows "undefined"

To get the Chrome/IE/old-Firefox behavior, the IDL needs to be:

  void alert();
  void alert(DOMString message);

with prose that says that the first overload uses "" as the message.

To get the Safari behavior, the IDL can be:

  void alert(optional DOMString message = "undefined");

For now I'm switching Firefox to our old behavior over in https://bugzilla.mozilla.org/show_bug.cgi?id=999315
Comment 2 Boris Zbarsky 2014-05-13 17:35:38 UTC
But note that the Safari behavior here is more JavaScripty in principle.
Comment 3 Domenic Denicola 2014-05-13 17:39:50 UTC
Just to chime in:

Both the Firefox 28+ and Safari 7+ behaviors are reasonably JavaScriptey. Firefox 28+ behaves as if it were

function alert(message = "") {
  message = String(message); // standard type coercion here
}

and Safari 7+ behaves as if it were

function alert(message) {
  message = String(message);
}

If we can get away with either of those two that would be nice. Sounds like the Firefox 28+ behavior is a no-go though.
Comment 4 Boris Zbarsky 2014-05-13 17:57:31 UTC
Right, to be clear I was comparing the Safari behavior to the Chrome/IE/old-Firefox behavior.

The new-Firefox behavior is indeed reasonably JavaScripty, but seems to not be what people seem to want from this API.
Comment 5 Boris Zbarsky 2014-05-13 17:57:56 UTC
And I personally would be OK with trying to switch Gecko to the Safari behavior.
Comment 6 Ian 'Hixie' Hickson 2014-05-13 18:00:32 UTC
If I define:

  void alert();
  void alert(DOMString message);

...and then you pass the undefined value explicitly, it picks the DOMString overload? It doesn't pretend the argument was omitted?
Comment 7 Boris Zbarsky 2014-05-13 18:28:09 UTC
I believe that is the current state of WebIDL, yes, and some other APIs rely on that....

Of course if we do the Safari behavior it's a nonissue.
Comment 8 Ian 'Hixie' Hickson 2014-05-15 21:02:06 UTC
Yeah, looks like Web IDL doesn't do anything to treat explicit "undefined"s as missing arguments. Ok.

Do you want this for prompt() or confirm() too, or just alert()?
Comment 9 Boris Zbarsky 2014-05-16 16:18:13 UTC
It's only come up as an issue for alert(), since people use that to examine the value.  For prompt() and confirm() both "" and "undefined" are pretty much pointless given how the string is supposed to be used, so I don't have an opinion as to which we should do.
Comment 10 Ian 'Hixie' Hickson 2014-05-16 16:53:37 UTC
Ok. Thanks!
Comment 11 contributor 2014-05-16 16:54:04 UTC
Checked in as WHATWG revision r8638.
Check-in comment: for compat, apparently alert() should alert '' and alert(undefined) should alert 'undefined'
http://html5.org/tools/web-apps-tracker?from=8637&to=8638
Comment 12 Ian 'Hixie' Hickson 2014-05-16 16:54:15 UTC
(I used the more widely implemented behaviour; I don't mind switching to another behaviour but we should get more vendors on board if we want to do that.)