如何从后面的代码中传递 JavaScript 函数中的自定义字符串?
How to pass custom string in JavaScript function from code behind in?
我有这样的字符串结构:
string str = "[['<h1>Heading 1</h1><p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>', 31, 32,1],
['<h1>Heading 1</h1><p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>', 34, 35,2]]";
我正尝试在 javascript 函数中传递此字符串,但警报不起作用。如何在 JS 函数中传递此字符串。到目前为止,我已经尝试过了,但没有用
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test", "test('" + str + "')", true);
JS
function test(str) {
alert(str);
}
您可以使用本机浏览器 JSON stringify 函数..像这样..
function test(str){
alert( JSON.stringify( str ) ) ;
}
你应该在弹出窗口中得到你的数组..
hth
当你检查生成的javascript时,它会以test('[['
开头——然后其余部分不被识别为字符串内容并给出javascript错误。
您需要转义字符串,使用 HttpUtility.JavaScriptStringEncode:
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test",
"test('" + HttpUtility.JavaScriptStringEncode(str) + "')", true);
我有这样的字符串结构:
string str = "[['<h1>Heading 1</h1><p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>', 31, 32,1],
['<h1>Heading 1</h1><p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>', 34, 35,2]]";
我正尝试在 javascript 函数中传递此字符串,但警报不起作用。如何在 JS 函数中传递此字符串。到目前为止,我已经尝试过了,但没有用
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test", "test('" + str + "')", true);
JS
function test(str) {
alert(str);
}
您可以使用本机浏览器 JSON stringify 函数..像这样..
function test(str){
alert( JSON.stringify( str ) ) ;
}
你应该在弹出窗口中得到你的数组..
hth
当你检查生成的javascript时,它会以test('[['
开头——然后其余部分不被识别为字符串内容并给出javascript错误。
您需要转义字符串,使用 HttpUtility.JavaScriptStringEncode:
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test",
"test('" + HttpUtility.JavaScriptStringEncode(str) + "')", true);