0

I want to know how to use this script in my case >>> https://stackoverflow.com/a/3890175/1503192 ...

This is my jsfiddle >>> jsfiddle.net/kZfGV/134/

HTML:

<body onLoad="linkify(inputText)">
    https://google.com/<br />       
    http://google.com/<br />
    https://www.google.com/<br />
    http://www.google.com/<br />
    www.google.com<br />
    www.google.com<br />
    admin@google.com
</body>

JS:

function linkify(inputText) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links.
    replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}

I have tried it in jsfiddle but i still can't figure it out before i tried it so many times in my blog. Also I've searching this on stackoverflow and google but nothing seem works. I am a newbie in this field. Plz help me guys. Thanks

Community
  • 1
  • 1
Suzylee
  • 31
  • 2
  • 8

3 Answers3

2

First of all you did not specifed what is input text in function linkify.

Second you do use returned value.

Working example is http://jsfiddle.net/T7ANY/

And the script looks like (without jQuery or any other plugin)

function linkify(inputText) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links.
    replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}

document.body.innerHTML = linkify(document.body.innerHTML)
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
2

http://jsfiddle.net/kZfGV/137/

html:

<span class="linkify">https://google.com/</span><br />       
<span class="linkify">http://google.com/</span><br />
<span class="linkify">https://www.google.com/</span><br />
<span class="linkify">http://www.google.com/</span><br />
<span class="linkify">www.google.com</span><br />
<span class="linkify">www.google.com</span><br />
<span class="linkify">admin@google.com</span>

javascript:

$(document).ready(function()
{
    $(".linkify").text(function(){
        return  linkify($(this).text());
    });
});
Daniel Gruszczyk
  • 5,379
  • 8
  • 47
  • 86
  • should'nt it be `$(".linkify").html( ...` ? – BiAiB Sep 03 '13 at 15:18
  • .html() will get all the dom from inside of the element, while .text() will get just the text and skips any other HTML inside of that element. Since I assumed we will be working on a pure text inside of these tags, .text() is fine. – Daniel Gruszczyk Sep 03 '13 at 15:20
0

try this:

  <!-- jquery and linkify includes here -->
  <script>
  $( function() {
    var body = $('body');
    body.html( linkify( body.html() ) );
  } )
  </script>
  <body> ...
BiAiB
  • 12,932
  • 10
  • 43
  • 63