10

all is not a built-in function or keyword, but why can I not call a function if it is named all?

There is no error message in the debug console, and the function works if I rename it to all2.

Here is the code: tested in chrome and IE10

<!DOCTYPE html>
    <head>
    </head>
    <body>
    <script>
        function all()
        {
            alert(1);
        }
        function all2()
        {
            alert(2);
        }
    </script>
    <input type="button" value="all1" onclick="all()">
    <input type="button" value="all2" onclick="all2()">
    </body>
</html>
CL So
  • 3,647
  • 10
  • 51
  • 95
  • 3
    Can you show us how you define and call the function? – ajp15243 Jan 02 '14 at 03:07
  • I can do it: http://jsfiddle.net/vNgwQ/ You should provide some more context (code, browser, OS, etc). – santiagobasulto Jan 02 '14 at 03:11
  • Please test my HTML file – CL So Jan 02 '14 at 08:20
  • 1
    all is not a reserved word according to MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words – Paul Jan 02 '14 at 08:31
  • 1
    I confirm success of the all2 button and failures of the all1 button with chrome 31.0.1650.63 (Developer Build 31.0.1650.63-0ubuntu0.13.10.1~20131204.1) Ubuntu 13.10 – Paul Jan 02 '14 at 08:44
  • 1
    Firefox on Ubuntu 13.10 also exhibits this behavior. Throws NS_ERROR_ILLEGAL_VALUE: Illegal value – Paul Jan 02 '14 at 08:57
  • 1
    After playing with this a bit, successful behavior can be had by changing the onclick handler to `... onclick="window.all();" >` – Paul Jan 02 '14 at 09:05
  • 1
    Thoughts: (1) scope problem with implicit eval? (2) saw something I didn't read about Microsoft adding .all() method to parts of the DOM which would be incompatible with other browsers. – Paul Jan 02 '14 at 09:07
  • 1
    Summary so far: You can define a `function all()` but calling it in an event handler set in an HTML string requires some attention to detail. My question is "why?" – Paul Jan 02 '14 at 09:12
  • 1
    replacing the event handler with "console.log(all);" in firefox all is pointing to document.all, and in chrome to an object with prototype HTMAllCollection – Paul Jan 02 '14 at 09:25
  • 1
    @adeneo Perhaps take another look at this `all()` question. – Paul Jan 02 '14 at 09:36
  • I think this is a good question. I never knew about document.all already existed. – Tanmoy Jan 02 '14 at 10:21
  • 1
    document.all is a proprietary Microsoft extension to the W3C standard. – Patsy Issa Jan 07 '14 at 03:20
  • 2
    Not the same question but [this answer](http://stackoverflow.com/a/2408447/1401094) explains it – Patsy Issa Jan 07 '14 at 03:22
  • @Paul: For whatever reason, `document` seems to be in the scope chain of inline event handlers. – Felix Kling Jan 07 '14 at 04:02

1 Answers1

7

This should have worked in chrome. However all has been a method in IE until IE11.

[all is no longer supported. Starting with Internet Explorer 11, use getElementById. For info, see Compatibility changes.] Returns a reference to the collection of elements contained by the object. via http://msdn.microsoft.com/en-us/library/ie/ms537434(v=vs.85).aspx

I remember using it long ago, early javascript days something like this..

for(i = 0; i < document.all.length; i++){
   document.all(i)   ...
}

It is deprecated in IE now and not implemented in most other browsers, although may still be considered a reserved name because of how wide reaching legacy code may be.

Update: I was able to track down another SO question, they answered it nicely.

document.all is available only on Internet Explorer, webkit and Opera.

On every other browser all is an undefined property of document object (and undefined is considered as a false value)

As historical note: many (really many) years ago document.all was used to tell Internet Explorer from Netscape Navigator so if you meet a script that is checking if (document.all) ... I strongly suggest to find a better script :)

-Fabrizio Calderan

Community
  • 1
  • 1
EHLOVader
  • 483
  • 4
  • 13
  • 1
    i believe most browsers implement it for compatibility with ancient and simple IE-specific sites actually; it was very common back in the day. – Eevee Jan 07 '14 at 04:01
  • agree with @EHLOVader, it works in Chrome - Try executing in Chrome dev tools console and it works- var all = function (a, b) { return a+b;} all(2,3); whereas I have no historical context of all on IE. – Soman Dubey Mar 12 '16 at 13:06