can't change the href of a link programatically, it reverts automatically....

Last post 07-29-2010, 9:58 PM by Kenneth. 1 replies.
Sort Posts: Previous Next
  •  07-27-2010, 7:41 AM 62739

    can't change the href of a link programatically, it reverts automatically....

    Hi there,
     
    I'm evaluating CuteEditor and I've encountered two problems. For now, I'll talk about the later because it is essencial to continue with CuteEditor.
     
    I have this text in a CuteEditor loaded from an aspx page:

    Once in the <a href="http://www.cnn.com/">far west</a>.

    On HTML mode, I then change the url from cnn.com to nbc.com.

    Back to normal mode I place the cursor in the middle of "far west" and then click a custom button which runs this code:

    var editor1 = document.getElementById(EditorID);
    var tempUrl = 'http://tempuri.org/tempuri.html';
    editor1.ExecCommand('insertlink',null,tempUrl);
    var links = editor1.GetDocument().getElementsByTagName('a');
    for (var i=0;i<links.length;i++)
    {
     if (links[i].href == tempUrl)
     {
      link = links[i];
      break;
     }
    }

    link.href = "http://www.cnn.com/";
    link.target = "abc";

    Then back to HTML mode and I get:

    Once in the <a href="http://www.nbc.com/" target="abc">far west</a>.

    And here is the problem: It changed the taget attribute but not the href attibute.
    If back in normal mode, I select the word "Once" and click the same custom button I get:

    <a href="http://www.cnn.com/" target="abc">Once</a>&nbsp;in the <a href="http://www.nbc.com/" target="abc">far west</a>.

    Which is the expected bhaviour.
    Can you please tell me what is wrong (with my code or yours) ??
    Many thanks and regards
    José Proença
  •  07-29-2010, 9:58 PM 62812 in reply to 62739

    Re: can't change the href of a link programatically, it reverts automatically....

    Hi ztp,
     
    Please try the example below
     
    1. <%@ Page Language="C#" %>   
    2.   
    3. <%@ Register Namespace="CuteEditor" Assembly="CuteEditor" TagPrefix="CE" %>   
    4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
    5. <html xmlns="http://www.w3.org/1999/xhtml">   
    6. <head id="Head1" runat="server">   
    7.     <title>Untitled Page</title>   
    8.   
    9.     <script runat="server">   
    10.         protected override void OnLoad(EventArgs e)   
    11.         {   
    12.             editor1.Text = "<a href='http://www.cnn.com'>far west</a>";   
    13.             base.OnLoad(e);   
    14.         }   
    15.     </script>   
    16.   
    17. </head>   
    18. <body>   
    19.     <form id="form1" runat="server">   
    20.         <CE:Editor ID="editor1" runat="server">   
    21.         </CE:Editor>   
    22.         <input type="button" value="ChangeLink" onclick="changeLink()" />   
    23.     </form>   
    24. </body>   
    25. </html>   
    26.   
    27. <script>   
    28. function changeLink()   
    29. {   
    30.     var editor1 = document.getElementById("<%= editor1.ClientID %>");   
    31.     var tempUrl = 'http://tempuri.org/tempuri.html';   
    32.     editor1.ExecCommand('insertlink',null,tempUrl);   
    33.     var links = editor1.GetDocument().getElementsByTagName('a');   
    34.     for (var i=0;i<links.length;i++)   
    35.     {   
    36.         if (links[i].href == tempUrl)   
    37.         {   
    38.             link = links[i];   
    39.             break;   
    40.         }   
    41.     }   
    42.     link.setAttribute("href_cetemp","http://www.cnn.com/")   
    43.     link.target = "abc";   
    44. }   
    45.   
    46. </script>  
     
    Regards,
     
    ken
View as RSS news feed in XML