JavaScript weirdness: “ReferenceError XXX is not defined”

If you’re getting a “ReferenceError XXX is not defined”-error while calling JavaScript functions (which definitive exist), you might want to check for any for each(…) – constructs in your source.
In case you use this way of iterating through a sequence, the browser will disrupt parsing parts of your JavaScript code, which leads to the error message above, if you use a different browser than Firefox.

I was bangin my head for the past 3 hours against my desk just to find out that the for each-expression only exists in Firefox (seems be some proprietary implementation) but not in all other browsers like Safari, Chrome etc…

So, in case you have something like this:

for each (var item in myArray){
   doSomething(item);
}

…you might want to replace it with this:

for (var i=0; i<myArray.length; i++){
   var item = myArray[i];
   doSomething(item);
}

One more thing: This won’t have to be the cause for your problems. Many JavaScript engines simply abort executing your code without any further notification on many kind of errors. The above solution is just one of the many, many cases that can lead to this kinda problems, but since it’s such a nasty one, I thought it would be worth to post it here.

God, this is one of the reasons why I dun really like JavaScript that much.. Every browser manufacturer cooks his own soup..

Tip: If you have troubles running a JavaScript program that runs on Firefox, but not on other browsers, you can try using Google Chrome, which has a quite decent JavaScript debugger built-in. To open just hit alt+cmd+J (on Mac OS).

Follow me on social media

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.