Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    93
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Change plain text to URL

    Hello all,

    I have the following piece of code which changes any plain text to a clickable URL -

    Code:
    jQuery.fn.mailto = function () {
      return this.each( function() {
        var re = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
        $(this).html( $(this).html().replace( re, '<a href="$1" target="_blank">$1</a>' ) );
        var re = /(([a-z0-9*._+]){1,}\@(([a-z0-9]+[\-]?){1,}[a-z0-9]+\.){1,}([a-z]{2,4}|museum)(?![\w\s?&.\/;#~%"=\-]*>))/g;
        $(this).html( $(this).html().replace( re, '<a href="mailto:$1">$1</a>' ) );
        var re = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
        $(this).html( $(this).html().replace( re, '$1<a href="http://$2" target="_blank">$2</a>' ) );
      });
    };
    The problem I have is that occasionally, the tags to make the text clickable are already provided, and sometimes they're not. If they ARE provided, then the HTML tags are printed as well as making the link clickable.

    I am looking for a way of changing the code, so that -
    • if the text already has the tags, then don't do anything (so it will be clickable when published and no further action is required)

    • if it doesn't have the tags, then add them on.


    I've experimented with if statements to no avail, I hope this makes sense.

    Any help appreciated.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    jQuery.fn.mailto = function () {
      return this.each( function() {
        if ( ($(this).html()).indexOf('<') == -1 ) {
            var re = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
            $(this).html( $(this).html().replace( re, '<a href="$1" target="_blank">$1</a>' ) );
            var re = /(([a-z0-9*._+]){1,}\@(([a-z0-9]+[\-]?){1,}[a-z0-9]+\.){1,}([a-z]{2,4}|museum)(?![\w\s?&.\/;#~%"=\-]*>))/g;
            $(this).html( $(this).html().replace( re, '<a href="mailto:$1">$1</a>' ) );
            var re = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
            $(this).html( $(this).html().replace( re, '$1<a href="http://$2" target="_blank">$2</a>' ) );
        }
      });
    };
    or
    Code:
    if ( ($(this).html()).indexOf('<a') == -1  && $(this).html()).indexOf('<A') == -1) {
    or even
    Code:
    if ( $(this).html() == $(this).text() ) {
    but it's getting late and I haven't tested

    Added Or use jQuery's contains, find, etc..
    Last edited by AndrewGSW; 08-23-2012 at 12:57 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    swiltch (08-23-2012)

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi everyone. I studied javascript and stuff like this in high school but there's now existing new ways to do the same thing that java does without using complicated codes. Google can help a lot, hope this helps.

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    93
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Code:
    jQuery.fn.mailto = function () {
      return this.each( function() {
        if ( ($(this).html()).indexOf('<') == -1 ) {
            var re = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
            $(this).html( $(this).html().replace( re, '<a href="$1" target="_blank">$1</a>' ) );
            var re = /(([a-z0-9*._+]){1,}\@(([a-z0-9]+[\-]?){1,}[a-z0-9]+\.){1,}([a-z]{2,4}|museum)(?![\w\s?&.\/;#~%"=\-]*>))/g;
            $(this).html( $(this).html().replace( re, '<a href="mailto:$1">$1</a>' ) );
            var re = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
            $(this).html( $(this).html().replace( re, '$1<a href="http://$2" target="_blank">$2</a>' ) );
        }
      });
    };
    or
    Code:
    if ( ($(this).html()).indexOf('<a') == -1  && $(this).html()).indexOf('<A') == -1) {
    or even
    Code:
    if ( $(this).html() == $(this).text() ) {
    but it's getting late and I haven't tested

    Added Or use jQuery's contains, find, etc..
    That works perfectly for links that already have the HTML tags, but it's now not adding the <a href to the text without HTML, what would I need to add?

    EDIT: I'm guessing some sort if statement saying if it does not already have the <a href, then add it on. Tried adding that in as well to no avail.
    Last edited by swiltch; 08-28-2012 at 01:58 PM.

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Did your original code work?
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    New Coder
    Join Date
    Nov 2010
    Posts
    93
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Did your original code work?
    Yes my original post worked, so I'm trying to combine the two!

    Cheers, Rich.

  • #7
    New Coder
    Join Date
    Nov 2010
    Posts
    93
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Still haven't managed to solve this, trying to combine the two scripts.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •