-
-
Save nashingofteeth/bb2da1111bdfdfe57c14 to your computer and use it in GitHub Desktop.
url to link conversion with A tag protection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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