MrCarder
MrCarder

Reputation: 438

Javascript broken after moving from IE8 to IE11: "Object doesn't support property or method 'all' "

I was just forced into a browser upgrade (IE8 to IE11) while in the middle of testing. I've lost some essential functionality with some javascript that suddenly doesn't work in my .NET site.

This section of the code was written when I was in grade school, so I'm not extremely familiar with it, but what seems to be the problem is a call to form.all. I have to assume that call was built into javascript at some point - there's no definition for it in the code.

There are 7 "if statements" that use form.all and they are all written the same way:

if(form.all(cTag + "PersonNum") != null)
      form.all(cTag + "PersonNum").value = personNumber;

The error:

JavaScript runtime error: Object doesn't support property or method 'all'

In newer versions of JavaScript, is there a version of form.all that performs the same action? All I really need is for someone to point me in the right direction.

A weird note: the same JavaScript code IS working in production on IE11

EDIT Ok, I found a line that was minimized. It looks like form is a created variable.

var form = document.forms(0);

EDIT2 Compatibility view/mode was the solution after all. I had added our production site's domain to the compatibility list and didn't think about it; adding 'localhost' fixed the issue. You just have to set it to the right domain first for it to work :)

Upvotes: 1

Views: 2787

Answers (2)

RobG
RobG

Reputation: 147453

IE introduced an all property for certain DOM objects (e.g. document) but it was never part of any W3C standard. It allowed access to DOM objects by name or ID using:

var element = document(elementNameOrID);

or

var element = document[elementNameOrID];

that is, it is a property that could use the same syntax as a method. Neat. Some other browsers supported it for compatibility, but it pretty much went out of use with IE 6 (not sure when IE started supporting getElementById, I think it was IE 5). But IE continued to think name and ID attributes were the same thing until IE 8 in standards mode.

Support for all has been dropped from IE 11 in standards mode.

If form is a reference to a form element, and cTag + "PersonNum" is the name of a form control, then the simplest fix is to change:

form.all(cTag + "PersonNum").value

to

form[cTag + "PersonNum"].value

which takes advantage of named form controls being made properties of the form that contains them. This behaviour is standardised and supported by browsers from the very beginning (i.e. every where) and is future proof (it's not going to change).

Upvotes: 0

JoshBerke
JoshBerke

Reputation: 67108

Check the browser compatability mode when your running in production it's probally on IE8.

You can use obj.getElementsByTagName("*")

You could also add an All method to the prototype if it's not there.

Upvotes: 2

Related Questions