Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDNN Open Source...DNN Open Source...Provider and Extension ForumsProvider and Extension ForumsClientAPIClientAPIPrepare HTML for use in JavascriptPrepare HTML for use in Javascript
Previous
 
Next
New Post
5/20/2006 9:05 AM
 
Hi,

I'm using the ClientAPI to implement AJAX into my module. One of the things to do is to fill a DIV element with some new HTML that was retrieved from thhe server. This HTML has been entered by the user using the Rich Text Editor, so it can conatin carriagereturns, linefeeds, single quotes and all sorts of special characters. What is the easiest way to prepare this string to be used in javascript? I was looking for something like Server.HTMLEncode but with no luck so far.

Any help appreciated.


Web applications, DNN websites, modules, skins and support
 
New Post
5/20/2006 12:34 PM
 
I used the following code now. If anyone has better ideas, please let me know.

        Private Function GetJSEncodedString(ByVal text As String) As String
            Dim retval As String = text

            retval = Replace(retval, "\", "\\")
            retval = Replace(retval, "'", "\'")
            retval = Replace(retval, """", "\""")
            retval = Replace(retval, vbTab, "\t")
            retval = Replace(retval, vbCr, "\r")
            retval = Replace(retval, vbLf, "\n")

            Return retval
        End Function


Web applications, DNN websites, modules, skins and support
 
New Post
5/22/2006 10:25 AM
 

Here is the one the ClientAPI uses.  (ClientAPI.GetSafeJSString)

''' -----------------------------------------------------------------------------

''' <summary>

''' Escapes string to be safely used in client side javascript.

''' </summary>

''' <param name="strString">String to escape</param>

''' <returns>Escaped string</returns>

''' <remarks>

''' Currently this only escapes out quotes and apostrophes

''' </remarks>

''' <history>

''' [Jon Henning] 2/17/2005 Created

''' </history>

''' -----------------------------------------------------------------------------

Public Shared Function GetSafeJSString(ByVal strString As String) As String

If Len(strString) > 0 Then

Return System.Text.RegularExpressions.Regex.Replace(strString, "(['""\\])", "\$1")

Else

Return strString

End If

End Function

However, as you just pointed out to me, I do not cover linefeeds appropriately.  I am not sure that you need to cover tabs.  Also, I am no regular expression expert, so if someone wants to provide the right expression to cover the cases that SuperSKa notes above please post them here.


 
Previous
 
Next
HomeHomeDNN Open Source...DNN Open Source...Provider and Extension ForumsProvider and Extension ForumsClientAPIClientAPIPrepare HTML for use in JavascriptPrepare HTML for use in Javascript


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out