Greg
Greg

Reputation: 3063

Grouping all my .js into one file - good idea?

I have several scripts on my website. Would it make sense to group them all into one single file? Or is it preferable to keep them separate?

Upvotes: 0

Views: 1181

Answers (2)

b1_
b1_

Reputation: 2126

For production best:

  • load all frameworks minified from google CDN
  • collect all script for site in one file minify/compress it.
  • if minified file larger 256kB and some scripts use not on all pages strip until it will be 2 or 3
  • if script files more then 3 it is bad (most browsers load it step by step and block page for user)
  • post all scripts at bottom of html code

For fallback frameworks load (google can be down too)

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">window.jQuery || document.write('<script type="text/javascript" src="js/jquery.min.js"><\/script>')</script>

Upvotes: 2

PeeHaa
PeeHaa

Reputation: 72652

For production I would always group them into one file and compress and/or minify them. Browser can only (used to?) make 2 requests at the same time to the same domain.

If it is a onetime action you can use this website to minify the files. Other wise you would have to look into automating the process.

But please also keep your original uncompressed files for if you want to make changes at some point.

Upvotes: 5

Related Questions