Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two pieces of html code and show differences [closed]

Tags:

html

c#

Lets suppose I have this 2 htmls

var p1 ="<h1>Title</h1>";
var p2 = "<h2>Title changed</h2>";

I need to generate an email with a visual difference, just something like what source control systems do with the content changed/added/deleted.

Basically the email needs to say what changed, what was added, what was deleted.

Obviosly the htmls will be more complex, I just wanted to illustrate what I need to do.

However, I am not sure, at what direction should I look, what are my options?

like image 770
Luis Valencia Avatar asked Dec 06 '25 10:12

Luis Valencia


1 Answers

You could use John Resig's algorithm to compare two strings: http://ejohn.org/projects/javascript-diff-algorithm/

Just download and include jsdiff.js in your script and run it like this:

var p1 ="<h1>Title</h1>";
var p2 = "<h2>Title changed</h2>";

document.body.innerHTML = diffString(p1, p2);
like image 147
Emilio Rodriguez Avatar answered Dec 08 '25 02:12

Emilio Rodriguez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!