[Javascript] JavaScritpt simple compression code

Paul Novitski paul at novitskisoftware.com
Fri Feb 18 04:26:20 CST 2005


Eligio,

That's an interesting problem you've got there, and I'll be interested in 
reading suggestions from others.  I'm not aware that it's possible to 
include non-text characters in an URL, and most compression algorithms 
would change ASCII text -- characters in the range hex(20) to hex(7E) -- 
into bytes that fall outside this range.  Compression is possible when 
you're converting from a more limited character set to a broader one.  For 
example, say your text consists of only digits 0-9.  One easy compression 
method is to strip off the redundant hex(30) from each digit and double 
them up in each byte so each digit takes a nibble:

13 = hex(31 33) --> hex(13)
27 = hex(32 37) --> hex(27)

If your text consisted only of letters A-Z (hex 41-5A) that would only be 
26 unique values.  26 expressed in bits is binary 11010, so it takes 5 bits 
to express the whole alphabet.  If you build bytes out of these 5-bit 
chunks, then you could compress each group of 8 letters into 5 bytes (eight 
5-bit letters --> five 8-bit bytes).  However, these compressed bytes would 
not be ASCII.

In your case, I suspect that the text character set you're sending is in 
fact *larger* than the character set allowed in URLs, since many text 
characters such as  space (hex 20) get DEcompressed (urlencoded) to three 
characters -- %20, etc.


By the way, instead of using Microsoft.XMLHttp object which I assume 
doesn't work cross-browser, you could simply set the src of an image and 
accomplish much the same thing:

<img id="sender" src="" width="1" height="1" />
...
img#sender {visibility: hidden;}
...
function fSend( part , text )
{
var oImg = document.getElementById("sender");
oImg.src = "http://www.mydomain.com/foo.asp?part=" + part + "&text=" + text;
}

(Or create new image objects and insert them into the DOM.)  Wouldn't that 
accomplish the same thing but work cross-browser?

Paul


At 01:37 AM 2/18/2005, Eligio Morgado wrote:
>Hi all.
>
>I'm doing lot of work with *big* string variables.
>
>I know it can seems comfusing, but I need a JavaScript code for
>compressing the plain text inside this variables.
>
>Let me explain a little more my problem.
>
>Imagine a javascript code that use Microsoft.XMLHttp to query an ASP
>script. Something like:
>
>function fSend( part , text )
>{
>var http = new ActiveXObject( "Microsoft.XMLHttp" );
>http.open( "GET" , "http://www.mydomain.com/foo.asp?part=" + part +
>"&text=" + text, 0 );   http.setRequestHeader( "Content-Type" ,
>"application/x-www-form-urlencoded" );
>http.send( );
>};
>
>By this way, I can send information from JavaScript to an ASP page and
>then store it into the data base.
>
>The problem is that the text to send is very big (much more than a
>valid url length), so I need to split it and perform multiple fSend( x
>, x ).
>
>I cannot change this way of doing things, this works fine and I'm not 
>allowed.
>
>But I was wondering... If I can compress source text, then send it,
>and then uncompress on the ASP, I can save many http communication.
>
>
>Any help ? Thanks
>
>Eligio Morgado.
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list