The approach I used was simple, and a project that included much more text might justify a more advanced approach. But using these simple techniques, I was able to chop about 15% off my file size (and therefore off my hosting bill for that particular file).
var sentence="This is where music goes to die.";
var words=sentence.split(" ");
for (i=0; i<words.length; i++)
document.write( i+ words[i] +"<BR>");
Below is the output you get when that script runs. The number at the start is the element number of the array, so the array words contains "This", for example.
Here we've separated words where the spaces are, but you can use any character or sequence of characters.
question="What do you call a donkey with three legs?";
question="What do you get if you cross an elephant with a rhinoceros?";
You've kept the question and answer pairs together, while also taking advantage of the shorter way to declare arrays. When you need to use the data, you can then separate the question and answer by splitting at the bar character("|"). Note that the split command discards the separator, in this case the bar character.
There is an opposite to split: join will take the contents of an array and combine it into a string, using whatever separator you specify between the different data items.
var fruitlist=fruits.join(", ");
document.write("I like "+fruitlist);
This produces the following output. Note the space after the comma in both the code above, and in the resulting output:
By using split and join together, it's possible to create a search and replace routine.
The second line there creates a new string. It starts by separating the old string, using the word 'Spectrum' as the separator. The separator is removed automatically by the split command, which means the word 'Spectrum' is removed and the parts of the sentence on each side of that word are put into different array elements.
We then use join to combine all those array elements together into a single string again, but we use the separator 'Amstrad' between the different sentence parts. The end result is that the word Spectrum is replaced by Amstrad in the string. Here's the resulting output.
I compressed the text in my quotes widget by looking for frequently repeated sequences of words or characters and replacing them with symbols. It's a technique I first saw used in the Amstrad game Spellbound years back.
I used this text analyser to identify those words that were used most often. I also looked at sequences of letters that were frequently repeated and replaced those with symbols. The extent to which you'll be able to compress your text will depend on how many recurring patterns there are. In my case, the words writing, book and author came up very often.
For the purposes of an example, let's use this nursery rhyme, which has plenty of repetition in it:
A peck of pickled peppers Peter Piper picked
If Peter Piper picked a peck of pickled peppers
Where's the peck of pickled peppers Peter Piper picked ?
Now, here it is using a search and replace routine:
var sourcestring="12a4of3<br>A4of312<br>If 12a4of3<br>Where's the4of312?";
As more text is added, though, the compression routine starts to pay for itself. New words can be easily added to the search and replace routine, using punctuation symbols and short character combinations (eg !1, !2, !3) to replace recurring phrases in new text that's added.
You'll need to weigh up the circumstances in which this script is useful to you, but for applications that include a lot of natural language text, this script could create substantial savings.