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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Email encoding script

    Hi everyone

    I am working on/modifying a dom script to automatically convert e-mail addresses in the text format of username [at] domain [dot] com that are inside a pair of em tags into working mailto links.

    the script works by getting all em elements in the document, checking to make sure they aren't empty and also that they contain the string \s\[at]\s in them and then converts them to mailto links

    the problem im having is that the script is skipping over every other em tag, so it is only creating mailto links for the 1st, 3rd, 5th, etc. em tags I know that the problem occurs in the final line of the function

    Code:
          ems[i].parentNode.replaceChild( a, ems[i] );
    because i've added alerts through the code and i know that its cycling through all of the em tags correctly, but when it gets to that last line for some reason it is only replacing the odd ems.

    I appreciate any help anyone can give on this, thanks for reading it. (this is inside an external js file, hence the window.onload function)

    Code:
    window.onload = function() {
    	createmailtoLinks();
    }
    
    function createmailtoLinks() {
      var ems = document.getElementsByTagName( 'em' );
      for( var i=0; i<ems.length; i++ ){
        if( ems[i].firstChild &&
            ems[i].firstChild.nodeValue.match( /\s+?\[at]\s+?/g ) ){
          var str = ems[i].firstChild.nodeValue;
              str = str.replace( /\s+?\[(?:dot|period)]\s+?/g, '.' );  // replaces all .
              str = str.replace( /\s+?\[(?:at)]\s+?/g, '@' );          // replaces the @
              str = str.replace( /\s+?\[(?:dash|hyphen)]\s+?/g, '-' ); // replaces all -
          var a = document.createElement( 'a' );
              a.setAttribute( 'href', 'mailto:'+str );
              a.appendChild( document.createTextNode( str ) );
          ems[i].parentNode.replaceChild( a, ems[i] );
        }
      }
    }

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why <em>?

    I don't see a problem with your code. Could you show us the X/HTML that you are testing that function with?

    I appreciate any help anyone can give on this, thanks for reading it. (this is inside an external js file, hence the window.onload function)
    Good. Using body's inline onload attribute is a very bad practice.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.


  •  

    Posting Permissions

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