K
K
KOT3012018-03-23 17:10:48
JavaScript
KOT301, 2018-03-23 17:10:48

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
i4YPG.gif
this

<div id="text" class="status" onclick="this.contentEditable=true;" contenteditable="true"></div>

and script:
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);
    });
    });
    }

I can't understand why it doesn't work. can you tell me how to do something like this? Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question