none
JavaScript modifying href changes link text as well for mailto: protocol

    Question

  • I noticed that when a JavaScript appends to the href attribute of a link object that does link to some e-mail address using the mailto: protocol, and if the link text of that tag contains an @ symbol and no nested tags, then the link text will be updated to display the new contents of the href attribute.

    I created a demo page, and there is also a news thread.
    Is this some strange "feature"???
    Friday, August 3, 2007 9:26 AM

All replies

  • Hello,

    Yeah, very strange...

    ----------------------------------------------------

    Computer Forum | SUPERAntiSpyware
    Saturday, July 5, 2008 7:26 PM
  • I wish I could find the article/bug report, but this is a long standing known issue. :-(

    I can tell you that it is _NOT_ fixed in IE8 Beta 1 (currently out for testing).

    It also doesn't work in IE8 Beta 1 if you use the "super standards mode" and call

    Code Snippet

    .setAttribute('href', 'mailto:anyvalue@anyothervalue');



    I haven't tried this, but you might be able to do the following...

    Code Snippet

    var myLink = document.getElementById('mailLink');

    var initInnerHTML = myLink.innerHTML;


    myLink.setAttribute('href', 'mailto:newuser@newdomain?subject=testing');


    //try reseting the innerHTML if IE flubbed it

    myLink.innerHTML = initInnerHTML;




    Best of luck



    Monday, July 7, 2008 3:19 PM
  • No, that looks like a bug to me. 

    However, I suspect it happens because you don't have an "@" in the href.  If you put an "@" in the mailto: string, does it still happen? 

    I'm not trying to excuse this behavior, just wondering. 

    -jeff
    Monday, July 7, 2008 5:14 PM
  • I consider this to be a facility, not a bug
    Changing link-text according to href seems logical on mailto: links
    (text would be misleading othervise)

     

    If you want to overide this:

    Code Snippet

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
     <title>Save and restore Link-text when changing href</title>
    </head>
    <script type="text/javascript">
    function init()
    {
     var obj = document.getElementById('mailLink');
     var html = obj.innerHTML;
     obj.href = "mailto:bug@microsoft.com";
     obj.innerHTML = html;
     document.body.innerHTML += 'href is now: ' + obj.href;
    }

    </script>

    <body onload='init()'>
     <a id='mailLink' href="mailto:my.address">test@test.com</a>
     <br>
     <br>
    </body>
    </html>

     

     

    Thursday, July 24, 2008 11:13 AM
  • A "facility" eh?

    I disagree.  If the developer wanted to change the link text, they have an opportunity to do so.

    Often links are labeled for convenience like:

    Send Email to Group

    Where the group might change (users added/dropped), and the Subject/Body might change etc.

    I've also spotted this bug being tracked elsewhere...

    http://webbugtrack.blogspot.com/2008/07/bug-140-changing-mailto-links-error-in.html

    It is definately a bug!
    Thursday, July 24, 2008 2:16 PM
  • I'm glad this report has seen quite some activity lately. I'll reply to the last few posts.

    Personally I'm not interested in workarounds; I know how to work around the problem, but I'd prefer a single company fixing a single buggy piece of software instead of every developer out there working around this bug sop that customers will never see it and thus never notice just how buggy the program is. Thanks for all the effors for workarounds, though, as they might be useful to other readers of this thread.


    jeffdav wrote:

    If you put an "@" in the mailto: string, does it still happen?

    Yes it does. Removing the @ symbol from the link text avoids the issue, adding one to the original link href changes nothing.


    trosen wrote:

    I consider this to be a facility, not a bug
    Changing link-text according to href seems logical on mailto: links
    (text would be misleading othervise)

    I hate it when programy try to be smart, because they usually fail miserably at it.

    What is the logical thing to do depends very much on circumstances. In any case, if web deverlopers want to change both things, they have to do so explicitely, otherwise they won't conform with standards and they page will be broken in other browsers. So you can't rely on this "feature" in any case. And in cases where link text and the email address from the mailto href were different in the first place, changing the text doesn't at all seem logical to me.


    unique_username

    I've also spotted this bug being tracked elsewhere...

    Interesting, sure looks similar, but also somewhat different. for them it seems to make a difference whether or not the href attribute contains an @ symbol. For me, the change was due to an @ symbol in the link text. Maybe the difference is due to appending to an existing text versus setting a new link including a change of protocol.

    Thursday, July 24, 2008 3:59 PM
  • My point was that this seems to be intentional behavior ( and therefore not a bug)

    but I agree that it's not smart to mangle other attributes unless it's standard and documented

     

    In this case MS could just delete som lines of code to fix the discrepancy - no big deal and not to mutch to ask for

     

    > I hate it when programy try to be smart, because they usually fail miserably at itI hate it when programy try to be smart, because they usually fail miserably at it

     

    The world would be a bether place for developers without programmers like Tim Paterson, Linus Torvals and programmers in general 

    Thursday, July 24, 2008 6:02 PM