[thelist] Diff algorithm in JavaScript

Fernando Gomez fgomez at criba.edu.ar
Thu Nov 27 15:48:32 CST 2003

In a JavaScript application I'm developing, the user is prompted if he 
wants to save the changes he's made to the data in a (complex) data 
entry form, before switching to another task.

Given the complexity of the data entry form, I've just thought that it 
would be nice to offer him a list of the differences between the actual 
data and the original data, so that he can decide in case of doubt.

The data I'm taking about is compared not element by element of the 
form; instead I build a long string with all the data in the form, 
something like

   "data line 1\ndata line 2\ndata line 3\ndata line 4\n..."

and then compare this string with the original one.

So it seems what I'm needing here is some version of the "diff" 
algorithm, the one used to compare two text files, which will kindly 
tell me which lines are new, which ones have gone, and which ones have 

Do you know of something like this already available for JavaScript?

Perhaps it is worth mentioning that this form is highly dynamic (a lot 
of JS+DOM behind it). So the approach of comparing each pair of values 
corresponding to the same form element might not work, since the two 
versions of the form might have different structures.

(Please send your messages with a copy to fgomez at criba.edu.ar, since I'm 
on digest mode.)

Thanks for your help :)


More information about the thelist mailing list