Javascript 通过javascript从后面的代码访问变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2714546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:36:18  来源:igfitidea点击:

Access variable from code behind via javascript

asp.netjavascriptvb.netjson

提问by Narazana

I have the following code that I want to return to a variable "t" in javascript:

我有以下代码要返回到 javascript 中的变量“t”:

Code behind:

后面的代码:

Public Shared Function GetSomeText() As String
  Dim result = "This is from code behind"
  Return result
End Function

Caller variable in javascript:

javascript中的调用者变量:

//This is not working like that, I think
    var t = GetSomeText();

So, how can I make variable "t" get the "result" from Function GetSomeText from code-behind?

那么,如何让变量“t”从代码隐藏的函数 GetSomeText 中获取“结果”?

Thank you.

谢谢你。

回答by tvanfosson

Try this -- assuming that this a public method on the page. This will call the method GetSomeText() on the page class and then do a Response.Write() of the data to the page as it's being rendered. The result should end up between the single quotes in your javascript.

试试这个——假设这是页面上的公共方法。这将调用页面类上的 GetSomeText() 方法,然后在呈现页面时对页面执行 Response.Write() 数据。结果应该在 javascript 中的单引号之间结束。

 var t = '<%= GetSomeText() %>';

回答by SLaks

You need to write the string to a Javascript variable in server-side code, like this: (In a <script>block in the ASPX page)

您需要将字符串写入服务器端代码中的 Javascript 变量,如下所示:(在<script>ASPX 页面中的一个块中)

var t = "<%= GetSomeText() %>";

Note that you mustcorrectly escape it, like this: (Or using the AntiXSS Toolkit

请注意,您必须正确转义它,如下所示:(或使用AntiXSS Toolkit

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