“最巧妙的”添加JavaScript在asp.net页面页脚方式 [英] 'Neatest' way of adding javascript to a page footer in asp.net
问题描述
每当我想以编程方式添加JavaScript库,比如jQuery的为例,它通常涉及确保有一个占位符
在我的页面的页脚,然后调用$ C,将采取一个链接到src作为参数,并返回一个 htmlgeneric
控件,然后将其加入到这一占位符$ cbehind方法code>。
Whenever i want to add a javascript library programatically, say jquery for example, it generally involves making sure there is a placeholder
at the footer of my page, then calling a codebehind method that will take a link to the src as a parameter and return an htmlgeneric
control, which is then added to this placeholder
.
这是仍然做它最巧妙的方法,甚至与.NET 4.0的?
Is this still the neatest way to do it, even with .net 4.0 out?
推荐答案
我认为更好的办法是使用的RegisterStartupScript方法:结果
<一href=\"http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx\">http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx
I think a better way is to use the RegisterStartupScript method:
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx
和在更好的你的情况RegisterClientScriptInclude:结果
<一href=\"http://msdn.microsoft.com/en-us/library/kx145dw2.aspx\">http://msdn.microsoft.com/en-us/library/kx145dw2.aspx
And even better in your case RegisterClientScriptInclude:
http://msdn.microsoft.com/en-us/library/kx145dw2.aspx
编辑:结果
下面是RegisterClientScriptInclude的示例:
Here's a sample of RegisterClientScriptInclude:
if (!Page.ClientScript.IsClientScriptIncludeRegistered("myJsInclude"))
Page.ClientScript.RegisterClientScriptInclude("myJsInclude", "myJsFile.js");
EDIT2:结果
下面是一个包括的RegisterStartupScript的例子:
Here's a sample of an include with RegisterStartupScript:
string jsBlock = "<script src='myJsFile.js'></script>";
if (!Page.ClientScript.IsStartupScriptRegistered("myJsInclude"))
Page.ClientScript.RegisterStartupScript(typeof(string), "myJsInclude", jsBlock, false);
您应该增加之类的东西LANGUAGE =文/ JavaScript的为脚本标记,但为便于阅读,我没有添加他们。
You should add things like language="text/javascript" to the script tag, but for readability I didn't add them.
这篇关于“最巧妙的”添加JavaScript在asp.net页面页脚方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!