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 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Detecting anchor tag on hyperlinks

    Hey guys,

    I'm currently using the following piece of script to add an anchor tag to URL's when they are fed into a page dynamically

    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>' ) );
    });
    };
    This works fine, BUT the problem I'm having is that some links that are being fed into the page are already active (have the anchor tag already attached to them). This code then adds another anchor tag, which causes problems on the page.

    I'm looking for some help in modifying this code to detect that if a URL already has anchor tag then leave it alone, but if it doesn't then add it on so it becomes clickable. This code is adding the anchor tag to hyperlinks with the anchor already in place...

    I think this should be quite simple with the use of an if statement but I can't get my head around it.

    Any help is really appreciated!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    str.indexOf("<a")

    If the value is -1, the anchor tag is not there; if it is 0 or greater, it is.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,294
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Code:
    return this.each( function() {
    should be

    Code:
    return this.each( function() { if(this.tagName.toLowerCase()=="a"){return; }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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