Search code examples
javascriptinternet-explorerdocumentmozilla

"document.forms is not a function" with "document.forms(0)"


I am maintaining many old websites and CMS systems at my new job and run into an issue with one. The ASP site is using JavaScript and this line stopped working today (or slightly earlier than today):

document.forms(0)

Console would report Uncaught TypeError: document.forms is not a function. After Googling, I found that I can replace it with:

document.forms[0]

and continue moving forward, however, this website and files are littered with this reference.

Can anyone find explain what might have happen? Chrome, IE and Firefox are not working, was there an update to browsers or JavaScript? I can't seem to find document.forms(0) in my searches. Odd.


Solution

  • I will post the answer that I have thus far and like the comments mention, this is not a standard for all browsers, it will work in IE, but generally nothing else:

    document.forms(0) //IE only
    

    This is the proper way:

    document.forms[0] //All browsers (including IE)
    

    I was able to find ONE article (Mozilla developer network) that references the round-brackets (link) saying that document.forms(0) is IE-specific ways to access elements and document.forms[0] is the W3C web standards replacements.

    To confirm, I did some testing with BrowserStack and noticed that all versions of IE were working, nothing else worked except for Chrome version 15 and 16 (first two versions that BrowserStack has on their system for XP).

    Use the following code or your website will only be IE compatible: document.forms[0]

    Maybe my Client was always using IE and just switched to Chrome or something else. I haven't confirmed that yet, more does the site have any time of Analytics.