Automatically convert plain text URLs into link using jquery

url2link

Here I am going to introduce one very useful plugin in jquery to automatically convert plain text URLs into link using jquery. Some time you got text with lot’s of url’s and you need to automatically generate link from plain URLs so that visitor can easily goto source site. Here i found plugin urlToLink on github which help you to transforms URLs in text into HTML elements.
url2link

Follow below steps to automatically convert plain text url’s into link using jquery

Include the latest version of jQuery library and jquery.urlToLink.min.js script in your html page.

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous">script>
<script src="jquery.urlToLink.min.js">script>

Sample html paragraph with some dummy URLs

<p style="width:70%; margin:0 auto;">
Lorem Ipsum ( http://lipsum.com ) is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. (http://example.com ) It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
My Public NoteBook - http://www.iamrohit.in p>

Lorem Ipsum ( http://lipsum.com ) is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. (http://example.com ) It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
My Public NoteBook – http://www.iamrohit.in

Finally call urlToLink to link function on page and convert all plain text url’s into links.

<script>
$(function() {  
 $('p').urlToLink();
});
script>

If you want to open all links in new tab, you need to simply pass target attribute in above function see below example.

<script>
$(function() {  
 $('p').urlToLink({
   target : '_blank'
});
});
script>

See live demo and download source code.

See official page for more information

Leave a Reply

Your email address will not be published. Required fields are marked *

Top