views:

200

answers:

2

My dev environment is LAMP-based (Drupal); there are several JS (jQuery) files that are served up to provide various bits of functionality.

Until recently, things worked fine (well, IE6 was hideous but at least it worked). However, recently, one of my JS files has started to refuse to execute at all in IE6 and IE7.

If the file contains the following, I receive two alerts, reading 'HELLO' and 'GOODBYE':

alert('HELLO');
alert('GOODBYE');

However, when I add in the ready handler, I get exactly zero alerts:

alert('HELLO');
$(document).ready(function(){
alert('AWESOME!!!!!!!!!!!!');
}
alert('GOODBYE');

The odd thing is that my code works fine (e.g. we see 'AWESOME!!!...') in Firefox and IE8. Furthermore, this code used to run fine in IE6/7. TO make things even stranger, the jQuery in other JS files still executes without incident. Perhaps I have too many ready handlers? (I've only got about five... ?)

Any thoughts? I've been battling this for hours, and I have no idea what is going on. Thanks in advance for your help!

UPDATE Thanks to Alconja (below), we determined that the problem was due to a missing paren and semicolon. I fixed the problem in my small bit of test code and the alert()s executed as expected. However, when I went back to my full-scale codebase, the paren/semicolon pair was present. However, given what I had experienced with the short test harness (and the error that I inadvertently introduced while pruning my full-length code down to the test harness), I knew that the problem was due to a syntax error in the javascript. So, I simply went through the file, deleting increasingly large portions of the code until the alert() box showed up.

As was to be expected, the (original) problem was due to a syntax error. My (incorrect) code was:

 $.post(
  '../ajax/changeUrl',
  {
   url: url,
  },
  function(responseText, textStatus, xhr) {
   // console.log(this);
   // console.log(responseText);
   // console.log(textStatus);
   // console.log(xhr);
  }
 );

As you can see, there is an extra comma on the line reading 'url: url,'. Once I removed this comma, the script executed correctly. Of course, Firefox and other browsers didn't barf on this nearly as badly as IE did, and I do know why I inserted this error - it's a bad habit introduced by PHP's lenience with commas in array definitions (or perhaps parameter/variable lists in general as well)...

Anyway, it's fixed! So, thanks again, everyone! :)

+6  A: 

If that's the exact code you're trying to run, you're missing a close bracket & semi-colon... try:

alert('HELLO');
$(document).ready(function(){
    alert('AWESOME!!!!!!!!!!!!');
});   //close the ready function call & statement
alert('GOODBYE');
Alconja
Thank you so much! It's amazing how easy it is to overlook something so simple like this when you've been staring at a screen for ten hours... :)
@rinogo - do you use any versioning? A Diff probably would have prevented this.
karim79
A diff would have only helped if the closing `);` was placed there in the previous version.
MitMaro
An editor with brace matching might have helped highlight the issue. Also, always open and close your braces immediately whenever possible.
Soviut
+2  A: 

I know this has been answered, but I just want to share some advice. You can help avoid problems like this by tabbing at each open bracket. If we take your code and tab it:

alert('HELLO');
$(document).ready(
    function(){
        alert('AWESOME!!!!!!!!!!!!');
    }

    alert('GOODBYE');

You can clearly see something is missing.

If you take the posted answer and tab it:

alert('HELLO');
$(document).ready(
    function(){
        alert('AWESOME!!!!!!!!!!!!');
    }
);   //close the ready function call & statement

alert('GOODBYE');

You can see it's perfect.

Appropriate tabbing really helps to avoid these issues :)

Noon Silk
Hi silky! Thanks for the suggestion. I am actually quite fastidious when it comes to whitespace and indentation - this was just a quick example of my problem...I'll explain more about what my REAL problem turned out to be in a comment above... (thanks again! :) )