javascript text compression/decompression

◇◆丶佛笑我妖孽 提交于 2019-12-22 08:25:04

问题


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.


回答1:


You can GZip the text file, and sent it to the browser. That way you wont have to do anything on the client side, the browser itself will decompress it.




回答2:


could you use HTTP compression?




回答3:


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.




回答4:


[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.




回答5:


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.




回答6:


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 :)



来源:https://stackoverflow.com/questions/994667/javascript-text-compression-decompression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!