Skip to content

Instantly share code, notes, and snippets.

@nashingofteeth
Forked from anonymous/1.html
Last active August 29, 2015 14:14
Show Gist options
  • Save nashingofteeth/bb2da1111bdfdfe57c14 to your computer and use it in GitHub Desktop.
Save nashingofteeth/bb2da1111bdfdfe57c14 to your computer and use it in GitHub Desktop.
url to link conversion with A tag protection
<div contenteditable>http://google.com</div>
<script>
document.onkeydown = function(evt) {
evt = evt || window.event;
if (evt.keyCode == 13) { // enter key
var text = document.querySelector('div').innerHTML;
var url = text.match(/(HTTP:\/\/)([a-zA-Z0-9.\/&?_=!*,\(\)+-]+)/i);
var foourl = url[0].replace("http", "foo");
document.foo = "foo";
document.http = "http";
var link = text.replace(url[0], "<a href='" +
foourl +
"' style='cursor:pointer' onclick='var http = this.href.replace(document.foo, document.http); window.open(http);return false'>" +
url[2] +
"</a>");
document.querySelector('div').innerHTML = link;
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment