2

I have a page which loads several scripts and I want to improve performance by delaying loading these scripts until the document is ready.

I implemented it with jQuery's getScript and it works, but I added logging of the result of script loading and turns out in a small percent of visits some of the external scripts fail to load. Now these scripts are essential for the page, so if they are not loaded then menus won't work and stuff.

I wonder if loading these scripts the conventional way via a static script tag (<script src="..."></script>), that is letting the browser do it is more reliable than doing it dynamically via getScript. I don't think there should be a difference, but I thought I'd ask.

Does the browser do the better job or is it all the same and these kind of script loading errors happen with the static script tag too, so getScript is as good as the script tag?

(I know it is the browser that does the downloading in both cases, the question is about the script tag vs getScript.)

Kelly Robins
  • 7,168
  • 6
  • 43
  • 66
Tom
  • 7,515
  • 7
  • 38
  • 54
  • Maybe the some browsers don't support AJAX requests? When you load things with script tag then every, even the oldest browser, will manage to download it with simple HTTP request. Maybe some of your clients browsers don't support any ajax features which you need to load your scripts with JS. – wesolyromek Mar 16 '14 at 21:02
  • The difference between the getScript and the script method is that with getscript your request does not get blocked (async loading) – DarkBee Mar 16 '14 at 21:06
  • @DarkBee yes, I know, that's why I switched to it. The question is if there is a difference in loading reliability or it's the same regardless of the method. – Tom Mar 16 '14 at 21:08
  • @wesolyromek I'm logging the error reasons too. Here are some of them: no reason is the most frequent, then there is "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE" from jquery, some DLL initialization errors, etc. The first one is the most frequent (no error reason given), so I don't know what happens there. – Tom Mar 16 '14 at 21:12
  • I have found some info, that it's a firefox error thrown when there's some unnamed bug :/ It often appears when people try to access local storage or some canvas methods. – wesolyromek Mar 16 '14 at 21:52
  • This depends on what "reliable" is to you. The downside to using getScript to load additional scripts is you have to be careful not to execute code that depends on those scripts until they are done loading, including additional dependencies. this is easy if all of your code is in once place, however, if you're code is broken up into modules, it may get difficult to stay organized without moving to something like requirejs that is built for this purpose. – Kevin B Apr 04 '14 at 14:32

1 Answers1

0

Take a look at this:

Why call $.getScript instead of using the <script> tag directly?

Also, if "turns out in a small percent of visits some of the external scripts fail to load," you need to hone-in on when and where that is happening. The issue may not be how you are calling the script, but when or where you are calling it from. See Wesolyromek's comment on your post, too.

Community
  • 1
  • 1
Joshua
  • 328
  • 1
  • 7