Reputation: 166112
Suppose I have a 400K text file which I want to read from a javascript. The problem is, my target audience have a slow connection, so 400k might take too long to load.
I suppose I need to compress the file, but well, how can I decompress it via javascript on the client side?
Is it worth it, or will the time needed for decompression negate the time saved in downloading?
UPDATE
Just to be clear, the file is text (data) not code.
Upvotes: 4
Views: 9709
Reputation: 3646
Yes you don't not have to handle compression/decompression yourself browser does it for you only you have to specify server parameter's for the server that you are using. Note: you can explicitly specify for what all MimeType( response-format's) Ex.
Below is tomcat server setting parameter.
<Connector port="8443"
protocol="org.apache.coyote.http11.Http11Protocol"
SSLEnabled="true"
maxThreads="200"
compression="on"
compressionMinSize="2048"
compressableMimeType="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=UTF-8,application/x-www-form-urlencoded,text/html,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json"
/>
To be sure that the compression is working you can check. press F12(developer's tool)[mine is Chrome] -> Network tab -> start recording. click the request url that is responding the required data/text. Then go to the header's tab.
you will see.
REquest/REsponse Headers
Content-Encoding:gzip
Accept-Encoding: gzip, deflate
that it using encoding. and the size column in the network tab againsr the url will be showing the reduced size.
Upvotes: 0
Reputation: 38102
[Old question, but just in case other people stumble across this...]
The best way to deal with this is to use HTTP compression. All major web servers and web browsers support this. In fact, if you're using a web hosting service and your file is a filetype that commonly requires compression (e.g. css, js, html), then it's probably already being compressed for transport and you're just not aware of it.
Upvotes: 0
Reputation:
DECOMPRESS JAVASCRIPT
Is IMPOSSIBLE to hide the compressed code.
A typical JavaScript compressed with /packer/ starts with the following code:
`eval(function(p,a,c,k,e,r)`…
`eval` can simply be replaced by `alert`.
The eval
function evaluates a string argument that contains JavaScript. In most packers, eval
is used, followed by document.write
.
To decompress JavaScript, replace these methods by one of the following:
1. Replace eval
by alert
(Like this: alert(function(p,a,c,k,e,r)
… and open or refresh the html page, the alert will simply print the code in a popup-window)
2. If the JavaScript appears after the <body>
element, you can add a <textarea>
like so:
`<textarea id="code"></textarea>`
Then, replace eval
(…); by document.getElementById("code").value=…;
From both options the first is more simple... Good luck :)
Upvotes: -3
Reputation: 2008
This looks interesting: http://rumkin.com/tools/compression/compress_huff.php
A few tests with a LOT of text turned up a pretty good result.
Upvotes: 1