1

I need for someone to be able to put some text into a page and then this gets sent to the server, saved in the database, and else where this text is put into a javascript variable.

Basically like this:

Write("var myVar=\""+MyData+"\";");

What is the best way of escaping this data? Is there anything out there already to deal with things like ' and " and new lines? Is base64 my only option?

My serverside framework/language is ASP.Net/C#

1
  • @Nick we may migrate in the future, but as of right now, no, we are still on 3.5 Commented Jun 14, 2010 at 16:47

1 Answer 1

0

You should use WPL:

Write("var myVar=" + Encoder.JavaScriptEncode(MyData, true) + ";");

if you don't want to reference the library, you can use the following function (adapted from the .Net source):

public static void QuoteString(this string value, StringBuilder b) {
    if (String.IsNullOrEmpty(value))
        return "";

    var b = new StringBuilder();
    int startIndex = 0;
    int count = 0;
    for (int i = 0; i < value.Length; i++) {
        char c = value[i];

        // Append the unhandled characters (that do not require special treament)
        // to the string builder when special characters are detected.
        if (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' ||
            c == '\\' || c == '\n' || c == '\b' || c == '\f' || c < ' ') {
            if (b == null) {
                b = new StringBuilder(value.Length + 5);
            }

            if (count > 0) {
                b.Append(value, startIndex, count);
            }

            startIndex = i + 1;
            count = 0;
        }

        switch (c) {
            case '\r':
                b.Append("\\r");
                break;
            case '\t':
                b.Append("\\t");
                break;
            case '\"':
                b.Append("\\\"");
                break;
            case '\\':
                b.Append("\\\\");
                break;
            case '\n':
                b.Append("\\n");
                break;
            case '\b':
                b.Append("\\b");
                break;
            case '\f':
                b.Append("\\f");
                break;
            case '\'':
            case '>':
            case '<':
                AppendCharAsUnicode(b, c);
                break;
            default:
                if (c < ' ') {
                    AppendCharAsUnicode(b, c);
                } else {
                    count++;
                }
                break;
        }
    }

    if (b == null) {
        b.Append(value);
    }

    if (count > 0) {
        b.Append(value, startIndex, count);
    }

    return b.ToString();
}
Sign up to request clarification or add additional context in comments.

4 Comments

You're "adapted" code is really rough. Getting it to work requires an AppendAsUnicode method. I found this though which is more complete(but looks just like yours) kooboo.svn.codeplex.com/svn/trunk/Everest.Library/Json/…
looking further it seems an exact rip with some slightly different variable names. Did you base this code off of that link above? I need to know because the license above is GPL.. I also notice you used it here too: stackoverflow.com/questions/2714546/…
ah wait, nevermind. koders.com/csharp/… is the source to the AjaxControlToolKit. Apparently the kooboo project ripped it out of there and then put it under their own copyright and a GPL license.... this code is actually under the Microsoft Public License though so doesn't matter..
I based it on the .Net Reference Source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.