hyperlink target property

Aug 7, 2008 at 7:00 PM
Is there a way to set the target property of a hyperlink?  I have been playing around with the HyperLinkDialog.js file to see if I could added something in there but, have not been successful.  Thanks for any help in advance!

-JD
Developer
Aug 14, 2008 at 4:17 PM
No, not at this time... Although you could switch to the HTML tab and add Target properties that way. Not a great solution but it's a work-around.
Aug 14, 2008 at 5:08 PM
yorkrj-

I did figure out a solution.  In RteStartup.js, inside of the SetHyperLink function I added the following code:


function SetHyperLink(v)
{
var elNode = null;

    if (v)
    {
       
        if (browser.isMicrosoft)
        {
            SetSelection();

            if (v.link && v.link!="")
            {
                richeditor.ExecRichEditCommand("CreateLink",v.link);   
               
           
           
                if (v.nofollow)
                {
                    sel.collapse();
                    elNode = sel.parentElement();
                                                     
                    while (elNode!=null && elNode.tagName!="A")
                        elNode = elNode.parentElement
               
                    /****************************************/
                    //Added to add target property to the link
                    var newAttr = document.createAttribute("target");
                    newAttr.nodeValue = "_blank";
                    elNode.setAttributeNode(newAttr);
                    /****************************************/

                 
                    if (elNode)
                        elNode.rel = "nofollow";
                       
                   
                }
                else
                {
               
                    sel.collapse();
                    elNode = sel.parentElement();
                                                     
                    while (elNode!=null && elNode.tagName!="A")
                        elNode = elNode.parentElement
                       
                    /****************************************/
                    //Added to add target property to the link
                    var newAttr = document.createAttribute("target");
                    newAttr.nodeValue = "_blank";
                    elNode.setAttributeNode(newAttr);
                    /****************************************/

                }
               
               
            }
            else
                 richeditor.ExecRichEditCommand("Unlink",null);
        }
        else
        {
            if (v.link && v.link!="")
            {
                var selectedRange = richeditor.GetWindow().getSelection();               
                var curDoc = null;
               
                richeditor.ExecRichEditCommand("CreateLink",v.link);   
               
                elNode = selectedRange.focusNode.parentNode;
               
                while (elNode!=null && elNode.tagName!="A")
                    elNode = elNode.parentNode;

                /****************************************/
                //Added to add target property to the link
                //gets the current document to create
                //the new atribute from                
                curDoc = elNode.parentNode;
                while ( curDoc.nodeType!= 9)
                    curDoc = curDoc.parentNode;
                /****************************************/


                 if(elNode != null)
                 {
                    /****************************************/
                    //Added to add target property to the link
                    var newAttr = curDoc.createAttribute("target");
                    newAttr.nodeValue = "_blank";
                    elNode.setAttributeNode(newAttr);
                    /****************************************/

                      var nextElement = elNode.nextSibling;
                      var spcNode =richeditor.GetDocument().createTextNode(" ");
                     
                     
                     
                      if(nextElement == null)
                      {
                        elNode.parentNode.appendChild(spcNode);
                      }
                      else if(nextElement.nodeType != 3)
                      {
                        nextElement.parentNode.insertBefore(spcNode,nextElement);
                      }  
                      var range = richeditor.GetDocument().createRange();
                      range.selectNode(elNode.nextSibling);
                      range.collapse(true);

                      selectedRange.removeAllRanges();
                      selectedRange.addRange(range);
                 }
            }
            else
                 richeditor.ExecRichEditCommand("Unlink",null);
        }
    }
   
    if (browser.isMicrosoft && richeditor.EditMode == true)
    selCurrent = null;
}

This does not give the user an option about adding the attribute but I'm sure it can be wired up in the HyperLinkDialog.js to allow the user to select the target of the link.  Maybe you could add it to the next release of the RTE.

-JD



Developer
Aug 15, 2008 at 12:13 AM
JD,

Thanks for sharing your solution. I've added your code to a Work Item so it's in the pipeline to be added to a future release. If you want to see the feature added sooner, vote for the work item.

Thanks,
Rob