Follow this Blog!

May 5, 2011

f Comment

How Do You Combine Multiple Javascript Files Into One Single Javascript File?

Amazon Here's what I am trying to do. I have several Javascript files and I combine them into one via 'cat' command or something (refer to Insert Newlines With Unix 'cat' Command To Combine Multiple Files for how to combine several files into one). Now I run the website and the browser is complaining about the Javascript saying it contains errors! How come while combining multiple JavaScript files into a single JS file I get errors?

PROBLEM
Don't worry I'll get to the solution soon, but you need to know why this is happening first. The problem is when you combine the js files WITHOUT ending each js file properly you run the risk of violating Javascript syntax! At the end of the js source you have the leniency of not ending it properly but still having it work fine. When it's followed by another Javascript statement however you'll run into errors.

SOLUTION
Simple. Just make sure before you combine the Javascript sources every Javascript source ends with a semicolon (;). Again refer to Insert Newlines With Unix 'cat' Command To Combine Multiple Files for how to combine several files into one!

I suggest that you add newlines or spaces at the end of the source to make it more readable.
If you have any questions please let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael