gpt4 book ai didi

javascript - 如何将脚本正确添加到控件?

转载 作者:行者123 更新时间:2023-11-30 00:04:52 25 4
gpt4 key购买 nike

我有这个代码:

Dim script = New HtmlGenericControl("script")
script.Attributes.Add("type", "text/javascript")
script.InnerText = "var alohaInterval = setInterval(function() {if (typeof Aloha === ""undefined"") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery("".editable-content"").aloha();})}, 100);"
htmlControl.Controls.Add(script)

这会生成以下内容:

<script type="text/javascript">var alohaInterval = setInterval(function() {if (typeof Aloha === &quot;undefined&quot;) {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery(&quot;.editable-content&quot;).aloha();})}, 100);</script>

问题在于它是 HTML 编码的。我的问题是:如何确保脚本不会被 HTML 编码?

最佳答案

根据您的需要,您可能希望通过多种方式来解决此问题。

对于您始终希望可用于控制的少量脚本,您最常将其作为字符串嵌入代码中,您将需要使用 RegisterClientScriptBlockRegisterStartupScript .选择哪一个主要取决于您希望在何处呈现脚本。您可以找到关于何时选择一个或另一个的很好解释 here .

您可能想要选择的另一个选项是 RegisterClientScriptResource .当我编写具有大量 JavaScript 的服务器控件或我想将其作为类库的一部分包含时,我发现此选项最有用。使用此选项,您可以将脚本作为嵌入式资源编译到库中,然后将库包含在其他项目中。当我拥有的不仅仅是几行 JavaScript 时,编辑体验会好很多。

对于这个特殊情况:

ClientScript.RegisterClientScriptBlock(Me.GetType, "alohainit", "var alohaInterval = setInterval(function() {if (typeof Aloha === ""undefined"") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery("".editable-content"").aloha();})}, 100);", True)

关于javascript - 如何将脚本正确添加到控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com