开发者

strange behaviour of static method

I load a js variable like this:

var message = '<%= CAnunturi.CPLATA_RAMBURS_INFO %>';

where the static string CPLATA_RAMBURS_INFO i put like:

public static string CPLATA_RAMBURS_INFO = "test";

I use it very well in this method.

 <script type="text/javascript">
    var categoryParam = '<%= CQueryStringParameters.CATEGORY %>';
    var subcategoryParam = '<%= CQueryStringParameters.SUBCATEGORY1_ID %>';
    var message = '<%= CAnunturi.CPLATA_RAMBURS_INFO %>';

    function timedInfo(header) {
        $.jGrowl(message, { header: header });
    };
</script>

so the message appears.

I do not undersand, why, iso of "test", if i take the value from a static method, ths 开发者_开发问答use of message js var is no longer succesfull (the message no longer appears).

public static string CPLATA_RAMBURS_INFO = getRambursInfo();

public static string getRambursInfo()
{
   return System.IO.File.ReadAllText(PathsUtil.getRambursPlataFilePath());
}

EDIT: Source Code:

<script type="text/javascript">
    var categoryParam = 'category';
    var subcategoryParam = 'subcategory1Id';
    var message = 'Lorem ipsum dolor sit amet, eu curabitur venenatis 

viverra pellentesque tortor tempor, nam est suspendisse, aenean vestibulum, suspendisse eget metus aenean at dictum nulla. In luctus, neque porttitor suscipit nibh, aenean ut, commodo velit leo volutpat ullamcorper. ';

    function timedInfo(header) {
        $.jGrowl(message, { header: header });
    };
</script>


Your file contains special characters (probably a newline or a ') that cause the rendered Javascript to contain a syntax error.

You need to escape the string using the Anti-XSS Toolkit, like this:

var message = '<%= AntiXss.JavaScriptEncode(CAnunturi.CPLATA_RAMBURS_INFO) %>';

EDIT: If AntiXss doesn't help, try the following function:

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();
}


Try to assign value to your variable in Page_Load() function. I think it'll work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜