Set HTMLEditorExtender to open links in a new window

999 Views Asked by At

I am trying to use the Ajax Toolkit HTMLEditor (June 2012 release) to improve a strictly internal application used to post news items and links (mostly to internal resources). Everything works very well except that I have been unable to get the link creator to create the links to open in a new window (essentially with a 'target="_blank"' tag).

I have exposed the source code tab and tried to add the 'target="_blank"' tag into the link manually, but that seems to break the HTML display, and my app shows the raw HTML code rather than the rendered HTML. If I use the link creator and don't alter the link it creates, everything works fine, except of course the link opens in the same window. Sanitizer on or off makes no difference.

So far I have not been able to uncover any doucmentation on how to accomplish what I am trying to accomplish. Any ideas out there?

1

There are 1 best solutions below

0
On

OK, figured it out, and now I feel dumb. All that was required was to use a regex to find the link pattern and replace it with with a pattern containing the target="blank" tag. I just feed the text generated by the extended text box into the following function:

Protected Function addLinkTarget(ByVal strText As String) As String
    Dim pattern As String = "(?<start><a[^>]*)(?<end>>)"
    Dim repl As String = "${start} target=""_blank"" ${end}"
    strText = Regex.Replace(strText, pattern, repl)
    Return strText
End Function

Or, in C#

protected string addLinkTarget(string strText)
{
    string pattern = "(?<start><a[^>]*)(?<end>>)";
    string repl = "${start} target=\"_blank\" ${end}";
    strText = Regex.Replace(strText, pattern, repl);
    return strText;
}