1

I've recently learn that it's possible to @import .js files in a manner similar to css files (see S.O. Q/A here).

Now the task of knowing when and when not to @import.

It seems answer's a matter of the pros/cons between number of page requests and size of the requests. Should only large .js files be added to pages conditionally and smaller ones globally imported? If so, what is a good general file-size cut-off limit?

An example:
Should a script like Fit Vids .js (which unminified is 2.698 kb) be included via @import for a site that only has videos on some pages? Or should it be conditionally added (via manual methods or dynamic logic - is there any such script to conditionally load .js?) for only pages with videos?

Community
  • 1
  • 1
Isaac Gregson
  • 1,999
  • 1
  • 21
  • 31
  • `@import` can only be done with `.js` via a framework such as [**mixture**](http://docs.mixture.io/preprocessors), not with plain vanilla js – David Chase Mar 14 '14 at 05:39
  • right, but even if using a different method (many are given in [these answers](http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file/)) to achieve the same results, the question of "when and when not to?" still remains. – Isaac Gregson Mar 14 '14 at 05:45
  • what do you mean specifically? injecting scripts onto the page as opposed to including them via `` ? – David Chase Mar 14 '14 at 05:48
  • no, not injecting vs including. I mean combining into *one* global script file vs including via *multiple* script files (all using ``) – Isaac Gregson Mar 14 '14 at 05:54
  • more scripts per url=better gzip perf, but it would be nice to fire domcomplete without loading more than 100kb... – dandavis Mar 14 '14 at 06:07
  • hmm..."more scripts per url=better gzip perf" seems a bit counter-intuitive (at least to someone with primarily `css` experience). Are you saying that it's best to have multiple `.js` files so long as their totaled size is `< 100kb`? If so, perhaps you could include this as an answer with some supporting resources? – Isaac Gregson Mar 14 '14 at 06:34

0 Answers0