1

I have a webpage that includes 2 Javascript files. The second file requires the one before it. More specifically, in my 2nd Javascript file I am immediately using jQuery's .ajax() method. Is it possible in a very edge case scenario that jQuery may not be ready when I go to use it?

Note: I do not want to use $(document).ready() and I do not want to use libraries such as require.js or head.js. I just want to have confidence that jQuery will be there when I go to use it, and that Javascript's single-threaded nature will be respected even in edge cases.

BumbleB2na
  • 10,723
  • 6
  • 28
  • 30
  • As long as you don't use AJAX, scripts are executed synchronously, in the order in which they're encountered. (And even then they are, `readystatechange` is dispatched on the same thread as any other event.) Attributes like `async` and `defer` notwithstanding. – millimoose Feb 19 '13 at 23:34
  • As `$(document).ready()` is part of jQuery, if you can use that, you can use anything else in jQuery. – Jeff-Meadows Feb 19 '13 at 23:35

4 Answers4

5

If you have jQuery referenced before the file that needs to use the $.ajax() method, you can be confident that jQuery will be available for use.

And to be clear, $(document).ready() is not ensuring that jQuery is ready, but that the DOM is available ready to be manipulated.

flavian
  • 28,161
  • 11
  • 65
  • 105
Andorbal
  • 880
  • 5
  • 15
2

Yes, Javascript is strictly single threaded, and each script will be parsed before the next script gets parsed.

The only situation where the second script would run without the first one running first, would be if requesting the first script failed, but requesting the second script succeeded, but if the scripts fails to load you are pretty much screwed anyway.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Scripts included by <script> with the src attribute are loaded serially. Therefore if you have the setup

<script src="one-file"></script>
<script src="other-file"></script>

...one-file must finish downloading and executing before the document can continue parsing (i.e. before retrieval of the other file even begins). This is different with the defer and async attributes, but it is probable that you are omitting those.

If my answer is not peace-of-mind enough for you, I gleaned it from the HTML spec itself.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

Please read: http://api.jquery.com/category/events/document-loading/ , you're essentially setting when the code will run at the start of your page. It doesn't really matter if you want to use $(document).ready() , it's required, though most of us have seen it more as:

$(function() {};

I'm not sure if your concern is valid or not, you haven't really specified a problem you can't figure out yourself by testing the app, but maybe what you may want to do if you're experiencing jscript code executing before it's time in a multi-js file environment (again why not just use 1, esp. if you aren't familiar with the technicalities) is: How to load one JavaScript file from another? load jscript from your jscript shrug

$.getScript("another_script.js");
Community
  • 1
  • 1
RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • I know. It's a pretty open question because I had no means of testing this theory on all browsers since IE 5.5. A co-worker brought it up and I couldn't find any evidence of this online. – BumbleB2na Feb 19 '13 at 23:43