Answer the question
In order to leave comments, you need to log in
How to make an autolink?
Help to make autolink js? The task is to make hyperlinks in the text automatically clickable as in Google Docs, iCloud Pages and MS Word. Like
this
<div id="text" class="status" onclick="this.contentEditable=true;" contenteditable="true"></div>
function replaceURLWithHTMLLinks(text,re) {
return text.replace(re, function(match, lParens, url) {
var rParens = '';
lParens = lParens || '';
var lParenCounter = /\(/g;
while (lParenCounter.exec(lParens)) {
var m;
if (m = /(.*)(\.\).*)/.exec(url) ||
/(.*)(\).*)/.exec(url)) {
url = m[1];
rParens = m[2] + rParens;
}
}
return lParens + "<a onclick=window.open("+url+"); href='" + url + "'>"
+ url + "</a>" + rParens;
});
}
var text = document.getElementById('text');
var re = /(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/;
if (text.innerHTML.match(re)) {
text.onkeydown =(function() {
alert(replaceURLWithHTMLLinks(text.innerHTML,re));
text.onmouseover = (function() {
this.contentEditable = false;
this.innerHTML = replaceURLWithHTMLLinks(this.innerHTML,re);
});
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question