I have a particularly long javascript, and was wondering if its possible to compress it down, and have it extract itself at the client ?
Printable View
I have a particularly long javascript, and was wondering if its possible to compress it down, and have it extract itself at the client ?
why is it a problem?
if it makes the source too long, just make it an external file.
here's what you can try doing though:
delete all comments
rename variables to something shorter
rename functions to something shorter
get rid of whitespace
if you have many short lines like this:
a=1
b=2
etc.
change it to:
a=1;b=2 etc.
can't think of anything else
Its actually just a very long list of products and manufacturers.
I already have this done in ASP.Net, but now I'm adding some intelligence to a web-form, and allowing for some client-side processing to allow the client to produce more complex searches.
This is what the file looks like :
Code:var strMasterHeaders = new Array(26 );
strMasterHeaders[0] = '1_Backup Solutions';
strMasterHeaders[1] = '2_Boards (video, sound, .)';
strMasterHeaders[2] = '3_CAD Peripherals';
strMasterHeaders[3] = '4_CAD Software';
strMasterHeaders[4] = '5_Connectivity (hub, switch, router,.)';
... etc
var strSecondHeaders = new Array(100 );
strSecondHeaders[0] = '37_14 -Inch Monitor';
strSecondHeaders[1] = '38_15 -Inch Monitor';
strSecondHeaders[2] = '39_17 -Inch Monitor';
strSecondHeaders[3] = '40_19 -Inch Monitor';
strSecondHeaders[4] = '41_20 -Inch Monitor';
strSecondHeaders[5] = '42_21 -Inch Monitor';
strSecondHeaders[6] = '10_Architecture & Design';
strSecondHeaders[7] = '1_Backup Software';
strSecondHeaders[8] = '12_Bridges & Routers';
strSecondHeaders[9] = '95_Bulk Storage Devices';
strSecondHeaders[10] = '11_CAD Retail';
strSecondHeaders[11] = '3_Card Accessories';
... etc
var strManufacturers = new Array(82 );
strManufacturers[0] = '3Com';
strManufacturers[1] = 'ACCO brands';
strManufacturers[2] = 'Acer';
strManufacturers[3] = 'Adaptec';
strManufacturers[4] = 'Adobe';
strManufacturers[5] = 'Allied Telesyn';
strManufacturers[6] = 'Antec';
strManufacturers[7] = 'AOC';
strManufacturers[8] = 'AOpen';
... etc
all the semi colons at the end of the lines can go if you're not planning on combining lines.
those variable names can definately be shortened. most people prefer long names (including me) but if you're desperate to make it shorter, that's one option.
also
a=new Array()
a[0]="hello"
a[1]="bye"
can be shortened to:
a=new Array("hello","bye")
I'de leave it and shove it in an external file though.
hmmmm. I think I'll find a compressor.
Compress the data and then base64 encode it. On Load, I'll unencode, and uncompress.
Base64 adds one third to the size, though.
No, what you should do is set up your web server to support transfer compression, if the client does too it can use it. This is handled transparently and you don't need to worry.
Good idea. Do you know if IIS supports compression 'out-of-the-box' ?Quote:
Originally posted by CornedBee
Base64 adds one third to the size, though.
No, what you should do is set up your web server to support transfer compression, if the client does too it can use it. This is handled transparently and you don't need to worry.
Don't know anything about the Ivil Information Server ;)
Watch your tongue peasant ;)Quote:
Originally posted by CornedBee
Don't know anything about the Ivil Information Server ;)
Another option (probably too labour intensive to bother with!) is to replace duplicated data with constants, eg:
Code:var IM = ' -Inch Monitor';
strSecondHeaders[0] = '37_14'+IM;
strSecondHeaders[1] = '38_15'+IM;
strSecondHeaders[2] = '39_17'+IM;
strSecondHeaders[3] = '40_19'+IM;
strSecondHeaders[4] = '41_20'+IM;
strSecondHeaders[5] = '42_21'+IM;
I've already done that actually - well, the mostly duplicated items - i.e. manufacturer names are replaced with an index number...Quote:
Originally posted by si_the_geek
Another option (probably too labour intensive to bother with!) is to replace duplicated data with constants, eg:
Code:var IM = ' -Inch Monitor';
strSecondHeaders[0] = '37_14'+IM;
strSecondHeaders[1] = '38_15'+IM;
strSecondHeaders[2] = '39_17'+IM;
strSecondHeaders[3] = '40_19'+IM;
strSecondHeaders[4] = '41_20'+IM;
strSecondHeaders[5] = '42_21'+IM;