Tool to compress JavaScript file

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Tool to compress JavaScript file

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

http://javascriptcompressor.com/
Useful Javascript (already compressed)

* Prototype JavaScript framework 1.5.RC.0 - 21 KB compressed (original was 51 KB)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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 ;-)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

prototype seems to be more widely used.
scriptaculous & lightbox javascript components use prototype as base.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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:
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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({ ... })
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 !
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

And also on digg they are using jQuery :)
Post Reply