Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not name a JavaScript function `all`?

Tags:

javascript

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>
like image 869
CL So Avatar asked Jan 02 '14 03:01

CL So


1 Answers

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

like image 172
EHLOVader Avatar answered Nov 15 '22 14:11

EHLOVader