如何编写用于调用脚本的代码 [英] How to write the code for calling the scripts

查看:138
本文介绍了如何编写用于调用脚本的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在用下面的代码编写JavaScript,如下所示:

Hi,

I am writing the javascript in Code behind as follows:

string script = "<script language=" + Strings.Chr(34) + "javascript" + Strings.Chr(34) + ">" + "  alert(" + Strings.Chr(34) + msg + Strings.Chr(34) + ");" + "</script>";
       ClientScript.RegisterStartupScript(this.GetType(), "Message", script);




它在strings.chr中显示错误.谁能告诉我正确的代码是什么.

在此先谢谢您




It showing error in the strings.chr. Canany one tell me what is the correct code.

Thanks in advance

推荐答案

谁告诉您有StringsStrings.Chr?这可能是您以前的化身.

您是指仅带有Unicode代码点34的字符吗?这只是一个引号!

用途:
Who told you there is Strings and Strings.Chr? It''s probably from your previous re-incarnation.

Do you mean just a character with Unicode code point 34? This is just a quotation mark!

Use:
string script = @"<script language=""javascript""> and so on...";



符号"@"表示字符串文字的详细语法;在这种语法中,引号通过加倍来转义.在常规语法中,它被转义为\".

来自数字文字的字符表示其代码点可以作为(char)34获得.您在这里不需要它.

为什么不简单地引用C#引用而不是反复试验呢?如此适得其反!

另外,请避免对字符串(+)进行多次串联. Is是非常无效的,因为字符串是不可变的. (我必须解释为什么吗?)如果只连接常量,那没关系,因为它是在编译时完成的,但是如果您在两者之间使用函数,那么它将在运行时进行.

使用string.Format代替.如果串联的成员是静态未知的,请使用System.Text.StringBuilder.

避免使用任何立即常量,尤其是字符串.使用显式常量,资源,数据文件等.

—SA



The symbol "@" means verbose syntax for string literal; in this syntax, quotation mark is escaped by doubling it. In regular syntax, it is escaped as \".

A character from numeric literal meaning its code point can be obtained as (char)34. You don''t need it here.

Why not simply referring to C# reference instead of your trial-and-error? So counter-productive!

Also, avoid multiple concatenation of strings (+). Is is very ineffective because string is immutable. (Do I have to explain why?) If you concatenate just constants, it would not matter, because it''s done in compile time, but if you used function in between, it would be during run-time.

Use string.Format instead. If concatenated members are statically unknown, use System.Text.StringBuilder.

Avoid any immediate constants, especially strings. Use explicit constants, resources, data files, etc.

—SA


像这样使用它

Use it like this

string script = "<script language=''javascript''> alert(''" + msg + "'');</script>";<br />
ClientScript.RegisterStartupScript(this.GetType(), "Message", script);


这篇关于如何编写用于调用脚本的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆