PDA

View Full Version : Javascript Compression with PHP



fambi
03-06-2007, 01:54 PM
Dear JS Enthusiasts,

What is the best way to pack a JS file as much as possible using PHP?

Links would be appreciated.

Thanks

mburt
03-06-2007, 02:08 PM
To "pack a JS" file? What exactly do you mean by that?

lainlives
03-06-2007, 05:35 PM
the only way i could think of is to take out all unneccessary returns

jscheuer1
03-06-2007, 05:55 PM
Do you mean pack (compress its size)? Do you mean using PHP to do this or do you mean so that it takes up less space as an include or whatever? Javascript can be made to take up less space if variable and function names are shortened and unnecessary line breaks and punctuation are removed. Code can often be reduced more by reusing code that appears more than once and using ? : tests instead of if else tests.

It is usually not worth it to do this unless bandwidth is very low. The resulting file will be hard to read and edit.

One good practice with javascript is to keep scripts external and link them to the various pages using them (if more than one) via an external script tag (instead of an include or any other method). That way each script only needs to be cached once.