Skip to content

Instantly share code, notes, and snippets.

@morningtoast
Created May 16, 2014 13:53
Show Gist options
  • Save morningtoast/d6f8322bda77fa494090 to your computer and use it in GitHub Desktop.
Save morningtoast/d6f8322bda77fa494090 to your computer and use it in GitHub Desktop.
View source JS
// Get HTML from an element, then escape it and drop it into another element (pre)
function viewsource(sel) {
var source = document.getElementById(sel).innerHTML;
source = source.replace(/[<>]/g, function(m) { return {'<':'&lt;','>':'&gt;'}[m]});
source = source.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>');
document.getElementById("source").innerHTML = source;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment