|
-
Apr 7th, 2004, 06:05 AM
#1
Thread Starter
Retired VBF Adm1nistrator
Compress javascript source ?
I have a particularly long javascript, and was wondering if its possible to compress it down, and have it extract itself at the client ?
-
Apr 7th, 2004, 06:32 AM
#2
Frenzied Member
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
Have I helped you? Please Rate my posts. 
-
Apr 7th, 2004, 07:10 AM
#3
Thread Starter
Retired VBF Adm1nistrator
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
-
Apr 7th, 2004, 07:32 AM
#4
Frenzied Member
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.
Have I helped you? Please Rate my posts. 
-
Apr 7th, 2004, 07:48 AM
#5
Thread Starter
Retired VBF Adm1nistrator
hmmmm. I think I'll find a compressor.
Compress the data and then base64 encode it. On Load, I'll unencode, and uncompress.
-
Apr 11th, 2004, 06:04 AM
#6
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 11th, 2004, 05:44 PM
#7
Thread Starter
Retired VBF Adm1nistrator
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.
Good idea. Do you know if IIS supports compression 'out-of-the-box' ?
-
Apr 12th, 2004, 12:51 AM
#8
Don't know anything about the Ivil Information Server
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 13th, 2004, 05:00 AM
#9
Thread Starter
Retired VBF Adm1nistrator
Originally posted by CornedBee
Don't know anything about the Ivil Information Server
Watch your tongue peasant
-
Apr 13th, 2004, 05:33 AM
#10
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;
-
Apr 13th, 2004, 06:10 AM
#11
Thread Starter
Retired VBF Adm1nistrator
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;
I've already done that actually - well, the mostly duplicated items - i.e. manufacturer names are replaced with an index number...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|