travega
travega

Reputation: 8415

Javascript class "undefined" - IE7

Why is it any time I go to test frontend changes IE adds about 2/3 hours of headache to the process? Thats not my question BTW... My question is why do I get a FooBar is undefined when I define a JS class in a file thusly:

var FooBar = {
    field1: '',
    field2: '',
    someFunction: function(variable) {
        field1 = variable;
    }
}

...and try to call it from another JS file thusly:

FooBar.someFunction("Hello World");

...in IE7. In IE 8+, FF 6+, Chrome 5+ and Safari there are no issues. But IE 7 doesn't seem to like it. Any ideas?

Edits:

The files are load in the order they are laid out in this example.

Upvotes: 0

Views: 1790

Answers (2)

travega
travega

Reputation: 8415

@Baz1nga and @jfriend00 touched on the root cause here. There was another issue on the page and it is one I have never seen before.

Another error:

"Expected Identifier, string or number"

was being thrown as a result of crap in another script. The specific crap in question was a trailing comma (,) in a parameter list with no parameter value after. For instance:

someFunction(param1, param2, param3,);

Other browsers strangely seem to cope with this syntax...but IE7? BOOM, catastrophic syntax death!

Upvotes: 2

Baz1nga
Baz1nga

Reputation: 15579

a) Make sure there are no js errors that are being thrown in IE, there are some IE specific JS issues, turn on the option to show popup when js error occurs

b) make sure your class variable definition file is loaded first and then the usage of the class.. also modify the syntax a little:

var FooBar = FooBar || {
    field1: '',
    field2: '',
    someFunction: function(variable) {
        field1 = variable;
    }
}

c) lastly add some debug code to see if the file where you are using the class is being executed or not, add some alert statements.

setTimeout(function(){
alert("before calling foorbar");
FooBar.someFunction("Hello World");
alert("foobar called");
},500);

Upvotes: 1

Related Questions