Change href with @ in anchor text

Discussion in 'Javascript' started by delimiter, Sep 19, 2007.

  1. delimiter

    delimiter Guest

    Hi,
    I've just found a really peculiar problem with IE (v 6 and 7) and am
    hoping for some help!
    I have a javascript function (called immediately before the closing
    body tag) that gets all the anchors in a specified div and then will
    either update the href or add an onclick event depending on whether
    the href is currently set to pdf. If the anchor text contains the @
    symbol and there is text preceding the @ then the anchor text is
    displayed on screen as the newly updated href. If there is no text
    before the @ then it's fine.

    Cut down example code below:

    /// HTML ////
    <html>
    <head>
    <title>Test</title>
    <script type="text/javascript">
    function fnLinks() {
    var oContent = document.getElementById("content");
    if(oContent != null) {
    var oATags = oContent.getElementsByTagName("A");
    for (i=0; i<oATags.length; i++) {
    oATags.href= "http://www.google.com";
    }
    }
    }
    </script>
    </head>
    <body>
    <div id="content">
    <p><a href="test1.html">Test 1</a></p>
    <p><a href="test2.html">@test2.com</a></p>
    <p><a href="test3.html"></a></p>
    <p><a href="test4.html">@test4.com</a></p>
    <p><a href="test5.html"></a></p>
    </div>
    <script type="text/javascript">fnLinks();</script>
    </body>
    </html>

    Many thanks
    Helen
    delimiter, Sep 19, 2007
    #1
    1. Advertising

  2. delimiter

    Botan Guner Guest

    Hi,

    You can use indexOf to detemine the position of @ like;

    for (i=0; i<oATags.length; i++) {
    if( (oATags.href.indexOf("@")!="-1") ||
    (oATags.href.indexOf("@")!="0") {//-1 means not found in string 0
    means at the begining
    oATags.href= "http://www.google.com";
    }
    }
    Botan Guner, Sep 19, 2007
    #2
    1. Advertising

  3. delimiter

    kindy Guest

    when text in a is 'xxx@xxx', ie treat it as a mail link; so it
    synchronization the text when you modify the link's href.

    if(oContent != null) {
    var txt;
    var oATags = oContent.getElementsByTagName("A");
    for (i=0; i<oATags.length; i++) {
    txt= oATags.innerHTML;
    oATags.href= "http://www.google.com";
    oATags.innerHTML= txt;
    }
    txt= null;
    }
    kindy, Sep 19, 2007
    #3
  4. delimiter

    delimiter Guest

    Thanks very much - that's sorted it completely!

    On Sep 19, 12:28 pm, kindy <> wrote:
    > when text in a is 'xxx@xxx', ie treat it as a mail link; so it
    > synchronization the text when you modify the link's href.
    >
    > if(oContent != null) {
    > var txt;
    > var oATags = oContent.getElementsByTagName("A");
    > for (i=0; i<oATags.length; i++) {
    > txt= oATags.innerHTML;
    > oATags.href= "http://www.google.com";
    > oATags.innerHTML= txt;
    > }
    > txt= null;
    > }
    delimiter, Sep 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Richter

    anchor href as relative path

    Steve Richter, May 3, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    840
    Steve Richter
    May 3, 2005
  2. =?Utf-8?B?ZXN3YW5zb24=?=

    how do I change the href property to the web anchor element

    =?Utf-8?B?ZXN3YW5zb24=?=, Mar 26, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    724
    Harshal P
    Mar 28, 2006
  3. Soren Vejrum
    Replies:
    4
    Views:
    566
    Lasse Reichstein Nielsen
    Jul 5, 2003
  4. Christopher Nelson
    Replies:
    17
    Views:
    248
    Christopher Nelson
    Oct 31, 2005
  5. David
    Replies:
    1
    Views:
    308
    David
    Dec 6, 2006
Loading...

Share This Page