How to reproduce: Script error, "Object does not support this property or method."

Can anyone give me scripts (HTML / CSS / Javascript) that can reproduce this error on IE 7.0

? I am trying to fix this error on my page where I am getting this warning, but could not find the problem exactly. The line number also doesn't match the source.

I thought the best approach was to create a bug and then work on it gradually, rather than making some wild assumptions!

-4


source to share


5 answers


As Shog said, this error will be thrown when trying to call a method on an object that does not have this method.

This is most often caused by the object being null if you expect it, well, not null.

var myEl = document.getElementById('myElement');
myEl.appendChild(...)

      

The above example will throw this error if the "#myElement" does not exist.



If this only happens in IE and not Firefox, chances are you are using a method that IE does not support.

Decision? You can try the Script Debugger, but I never found it useful for myself. The most reliable, but painfully slow method, is debug () debug. At strategic points through your code, post a warning with a unique message. IE will throw an error at some point, so you'll know that the error is somewhere between the last warning you saw and the next one that didn't.

function myFunc() {
    alert('a');
    var myEl = document.getElementById('myElement');
    myEl.appendChild(something);
    alert('b');
    // more code
}

      

if you run this and you see "a" but not "b", you know it out there somewhere. I'm afraid this is just the sad state of javascript in IE.

+2


source


If you submit your code according to the jslint.com rules, it will disappear. It will also tell you where exactly the problem is in your code.



+1


source


I thought the best approach would be to create a bug and then work on it gradually, not a wild guess!

You are wrong. There are many scenarios that can cause the error you are seeing. All this means that you are trying to call an undefined method. For example:

var blah = {};
blah.methodWhichDoesntExist(); // <-- error

      

This could be the result of a typo, undetected failure in some other component, or gamma rays sequentially flipping bits in your HTML immediately after saving. It's hard to be more specific without knowing more about what you are doing.

+1


source


For IE, download the Microsoft Script Debugger

0


source


I just fixed a similar bug by using IE8 and switching it to IE7 mode. You will then receive a more specific error message stating which line in the file is failing.

After that, set a breakpoint so that the code stops just before the offending line (F12 to go to the debugger tools, then select the JS file you want from the scripts dropdown and click to the left of the line number. "Start debugging", to use breakpoints).

As others have pointed out, this error can have many reasons, but in my case, I had two ids on the same page with one header and one not. getElementById () was wrong. Only IE7 complained.

0


source







All Articles