views:

25

answers:

1

Hello, 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#

A: 

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();
}
SLaks
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) https://kooboo.svn.codeplex.com/svn/trunk/Everest.Library/Json/JSONHelper.cs
Earlz
@SLaks, 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: http://stackoverflow.com/questions/2714546/access-variable-from-code-behind-via-jqery/2714563#2714563
Earlz
ah wait, nevermind. http://www.koders.com/csharp/fidBFC9EC6A462D72BD2E6AA18F76B771781E8504F8.aspx?s=cdef%3Aajax 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..
Earlz
I based it on the .Net Reference Source.
SLaks