Page 1 of 1

Tool to compress JavaScript file

Posted: Mon Sep 17, 2007 1:48 am
by anjanesh
HI

Version 1.6 of prototypejs is 120KB - uncompressed !
Dont know why the site doesnt offer a compressed version.
Is there good tool out there that compresses (compacts, not zips) a JS file to a smaller size by removing newilnes, comments etc etc ?

Thanks

Posted: Mon Sep 17, 2007 6:05 am
by VladSun
http://javascriptcompressor.com/
Useful Javascript (already compressed)

* Prototype JavaScript framework 1.5.RC.0 - 21 KB compressed (original was 51 KB)

Posted: Tue Sep 18, 2007 2:31 am
by Kieran Huggins
http://dean.edwards.name/packer/

Speaking of which... jQuery is much smaller than prototype (and better, IMO!). Incidentally, jQuery uses packer for the compressed version, which also incidentally is available for download on their site ;-)

Posted: Tue Sep 18, 2007 2:50 am
by anjanesh
prototype seems to be more widely used.
scriptaculous & lightbox javascript components use prototype as base.

Posted: Tue Sep 18, 2007 4:16 am
by CoderGoblin
Have to say that I moved from prototype to jquery some time ago. I have found nothing in prototype/scriptaculous that has not been done in jquery and it's plugins with a smaller footprint. It may be simply that scriptaculous if found more readily on google than jquery (that's how I found prototype to begin with). Always remember more popular doesn't necessarily mean better. I could say compare Postgres and Mysql but that would start too long a debate and one that I would be outvoted on purely because everyone seems to use mysql :wink:

Posted: Tue Sep 18, 2007 4:26 am
by CoderGoblin
Found this on the Jquery site and JSLint may be of interest.
jquery FAQ wrote:How do I compress my code?

Generally the best way to do it is to use Dean Edwards' Packer (this is the same script that jQuery uses to compress its copy of the library).

An alternative is to use Douglas Crockford's JSMin. This doesn't compress JavaScript code as small, but generally how you write your code matters less. jQuery also provides a pre-minified version of jQuery for your convenience.

If compressing your JavaScript breaks it, try running the code through JSLint. This will detect minor errors that can cause packed JavaScript to fail while the unpacked version works fine.

Posted: Tue Sep 18, 2007 4:30 am
by s.dot
After recently first-time using jQuery today, and a previous user of scriptaculous.. the cross-browser compatibility issues are resolved very well in jQuery. Not so much in scriptaculous.

Posted: Tue Sep 18, 2007 10:53 am
by anjanesh
Odd, Im now using jQuery instead !!!

There are not enough docs/tutorials/examples on prototype. The best I could find was this.
So far jQuery is enough for my requirements - its 50KB compared to the 120KB prototype (readable versions).

But jQuery's docs is neat and can learn faster. I probably will check out prototype another time when more sophisticated requirements arise.

Btw,

Code: Select all

var xhr = getAJAXObject(); // XMLHttpRequest Variable
function PostCommment()
 {
        if (!xhr) document.getElementById("Form1").submit(); // AJAX error : switch to <form> submit

        // To do AJAX submit using $.ajax or whatever

        return false; // To stop the <form> being submitted, since its done via AJAX
 }

Code: Select all

function getAJAXObject() 
 {
        var xhr = false; 
        
        if (window.XMLHttpRequest) // native XMLHttpRequest object
         {
                try { xhr = new XMLHttpRequest(); }
                catch(e) { xhr = false; }
         }
        else if (window.ActiveXObject) // IE/Windows ActiveX version
         {
                try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
                catch(e)
                 {
                        try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
                        catch(e) { xhr = false; }
                 }
         }
        return xhr; 
 }
How do I know in jQuery if the ajax object isnt false or not ? Im not looking for the :error function.
I need to know before doing $.ajax({ ... })

Posted: Wed Sep 19, 2007 1:50 am
by Kieran Huggins
Another few recruits into the wonderful world of jQuery :-)

@anjanesh: not sure I understand exactly what you're asking. What are you trying to accomplish, exactly?

Posted: Wed Sep 19, 2007 2:36 am
by anjanesh
How can I know if $.ajax() has failed initializing ? Not talking abt error msgs after AJAX is done - Im talking abt AJAX not being able to initialize in the first place.

Btw, Checkout the source code of http://code.google.com/
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jq ... "></script>
Wow !

Posted: Wed Sep 19, 2007 1:19 pm
by kaszu
And also on digg they are using jQuery :)