Combine external javascript

> > How to combine external javascript

Updated: January 2nd 2016

JS JS JS

Combining javascript files

Javascript files are very often found in several external files. There is not always a need to have these files be separate as most javascript can be combined into one file and not affect the ways it functions.

Collecting all javascript files into one will really reduce the amount of time it takes to load your web pages because you are reducing the amount of things the web browser has to request before displaying your page.




Test your pages

Our new free javascript usage tool examines a page and lists all javascript files by size, and shows an overview of how javascript is used on a page.

Wordpress users pay attention

If you use Wordpress, I can almost guarantee that your blog is loading several (likely 6 or more) external javascript files. It does not have to be this way.

How to combine your javascript

You can often just copy and paste the contents of each javascript file together to make one main JS file.

For example, you may have a javascript file called "this.js" and another called "that.js" being called by your HTML. In this situation your could open your "this.js", copy and paste the contents of the file into "that.js".

Once you have done that, you would remove the call for "this.js" from your HTML. You now have just one JS file instead of two that a web browser must load before displaying your webpage. Your webpage will now load faster.

Javascript libraries

The main thing to watch out for is to remember to put javascript in the right order. As an example, let's say you are using jQuery and a few small scripts that require jQuery. You can combine all the js, but if the jQuery is not first, your js may not work as expected.

Other javascript pagespeed tactics to consider

You can also choose to inline small javascript or defer your javascript for even more savings as recommended 1 by Google.

Tool

To see see how javascript is used on a page, use the javascript usage tool.






Patrick Sexton by