[英]How to pass custom string in JavaScript function from code behind in?
I have a string structure like this: 我有一个像这样的字符串结构:
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]]";
I am trying to pass this string in javascript function but alert is not working. 我试图在javascript函数中传递此字符串,但警报不起作用。 How can I pass this string in JS function. 如何在JS函数中传递此字符串。 So far I have tried this but it is not working 到目前为止,我已经尝试过了,但是没有用
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test", "test('" + str + "')", true);
JS JS
function test(str) {
alert(str);
}
When you check the generated javascript, it will start with test('[['
- and then the rest is not recognised as string content and gives a javascript error. 当您检查生成的javascript时,它将以test('[['
-开头,然后其余字符不被识别为字符串内容,并给出javascript错误。
You will need to escape the string, using HttpUtility.JavaScriptStringEncode : 您将需要使用HttpUtility.JavaScriptStringEncode对字符串进行转义 :
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "test",
"test('" + HttpUtility.JavaScriptStringEncode(str) + "')", true);
you can use native browser JSON stringify function.. like this.. 您可以使用本机浏览器的JSON字符串化功能。
function test(str){
alert( JSON.stringify( str ) ) ;
}
you should get your array in popup.. 您应该在弹出窗口中获取数组。
hth 心连心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.