理解部分代码 [英] Understanding part of code

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

问题描述

好吧所以我是JavaScript的新手,并尝试理解这部分代码:

ok so i am new at JavaScript and trying to understand this part of code:

if (flag == 1)    //On successful updation, shows a popup message
                {
                    string msg = "Operation Successful";
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("<script type = 'text/javascript'>");
                    sb.Append("window.onload=function(){");
                    sb.Append("alert('");
                    sb.Append(msg);
                    sb.Append("')};");
                    sb.Append("</script>");
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
                }



我知道如果已插入数据,它会创建一个弹出窗口以显示Operation Successful消息。而Append是将所有这些元素附加在一起以创建一个大字符串。我真的很想知道


I understand that it's creating a pop-up to display the message "Operation Successful" if the data has been inserted. And Append is to append all those elements together to create 1 big string. I would really love to know about

ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());



这行是做什么的?我也不太确定


what does this line do? also i am not very sure about

sb.Append("window.onload=function(){");



非常感谢任何帮助!谢谢!


any kind of help is greatly appreciated! thanks!

推荐答案

RegisterClientScriptBlock将您在字符串构建器中构建的文本注入页面。如果要确保脚本位于页面的开头或底部,则不同的注册方法会将文本注入页面上的不同位置。如果您查看页面的来源,您将在源代码中看到您的脚本块。



window.onload =行没有任何内容,所有内容都是文本附加在一起所以完成的文本是



window.onload = function(){//其余的js在这里};



当查看页面时,这个js将创建一个带有警报的函数,并设置在调用window.onload事件时调用的函数,即当页面加载到浏览器中时。所以页面加载后你会看到一个警告。



RegisterClientScriptBlock的alert参数是关键。所以你插入的js给出了alert键,这意味着如果其他控件想要注入相同的js,他们可以检查是否已经添加了带有alert键的js,以便相同的代码不再添加。
RegisterClientScriptBlock is injecting the text you've built up in the string builder into the page. The different "Register" methods will inject the text in different locations on the page if you want to make sure the script is at the start of the page, or the bottom. If you view the source of the page you'll see your script block in the source.

The "window.onload=" line does nothing on it's own, all of the text is appended together so the finished text is

window.onload=function(){ // rest of js here };

when the page is viewed this js will create a function with the alert in it and sets that function to be called when the window.onload event is called, ie when the page has loaded in the browser. So after the page has loaded you'll see an alert.

The "alert" parameter of RegisterClientScriptBlock is the key. So the js you've inserted is given the key of "alert" which means that if other controls want to inject the same js they can check to see if js with a key of "alert" has already been added so that the same code is not added again.


1。 ClientScript.RegisterClientScriptBlock(this.GetType(),alert,sb.ToString()); 要理解这一点,您可以按照使用&调用RegisterStartUpScript,RegisterClientScript和Client-Side Script [ ^ ]



2. sb.Append(window.onload = function(){);

我认为你已经理解了字符串构建器部分。对于 window.onload = function(){,你可以按照这个。

https://thechamplord.wordpress.com/2014/07/04/using-javascript-window-onload-event-properly/ [<一个href =https://thechamplord.wordpress.com/2014/07/04/using-javascript-window-onload-event-properly/\"target =_ blanktitle =New Window> ^ ]

http://www.w3schools.com/jsref/event_onload.asp [ ^ ]
1. ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());To understand this you can follow Use & Call RegisterStartUpScript, RegisterClientScript and Client-Side Script[^]

2. sb.Append("window.onload=function(){");
I think you have understood the string builder part. For window.onload=function(){ you can follow this one.
https://thechamplord.wordpress.com/2014/07/04/using-javascript-window-onload-event-properly/[^]
http://www.w3schools.com/jsref/event_onload.asp[^]


第一个很容易解决: Google [ ^ ]将为您提供。



另一个也很简单:

sb是一个StringBuilder - 它允许你添加文本来构建你的总字符串(没有与连接字符串相关的惩罚)。你可以使用Append方法:

The first is simple to work out: Google[^] will give you that.

The other is also simple:
sb is a StringBuilder - it allows you to add text to it to build up your total string as you go (without the penalties associated with concatenating strings). You do that by using the Append method:
sb.Append("some string data);



在您的情况下,附加的数据是JavaScript函数声明。


In your case, the data being appended is a JavaScript function declaration.


这篇关于理解部分代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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