2

I'm currently working on a project where some bugs are popping up in our jQuery AJAX calls. In most of these cases we have some elements that need to be initialized to do a certain thing when they're clicked (or for some other event trigger). We've decided to fix some of these issues to go with inline scripts at the end of the html instead of relying on the $(document).ready event trigger.

This however led us to wonder what would happen if jQuery library itself (which is being loaded via google) wasn't loaded quickly enough. Particuarly with IE, will the browser intelligently wait to execute those lines of code until the jQuery library is loaded? Is a race condition in this case possible? So far most of our issues seem to present themselves in IE (even 8 and 9) or in a few cases Firefox.

EDIT: The change from using the $(document).ready event trigger was suggested in How can I ensure that a link's default action is always disabled? which is also related to this question's situation. It seems to be working well with the inline script at the bottom of the html. It is unclear at this time whether the issue is because jQuery isn't loading (or some other element) or if the $(document).ready event is never triggering properly.

Community
  • 1
  • 1
Kenneth
  • 589
  • 6
  • 18

2 Answers2

1

I don't see a reason why would placing ajax calls be better outside $(document).ready...actually $(document).ready waits for all files to be loaded...this is why it is used (of course, not the only reason)

So, as far as your jquery ajax calls are placed inside $(document).ready callback, you can be sure that all your js files are loaded prior to that, and that you should look for cause of your problems on some other place.

Community
  • 1
  • 1
Aleksandar Vucetic
  • 14,715
  • 9
  • 53
  • 56
  • What would happen however if jQuery was delayed in being loaded for some reason? Would $(document).ready fail since it relies on jQuery being loaded to make sense to the browser? – Kenneth Feb 09 '12 at 20:37
  • 1
    It cannot fail, as long as your – Aleksandar Vucetic Feb 09 '12 at 21:45
  • here is one link where the same thing is discussed: http://stackoverflow.com/questions/3231616/order-of-javascript-script-tag-executions-not-guaranteed-in-major-browsers – Aleksandar Vucetic Feb 09 '12 at 21:47
0

Some browsers load all javascript that is on the local server before loading cross-domain scripts. So it is possible that your scripts are loaded first before the jQuery library is loaded from Google. I would suggest using the $(document).ready trigger.

Michael Robinson
  • 1,985
  • 2
  • 21
  • 31