admin管理员组文章数量:1302360
How can I add an external script reference to the footer? I know how to add it to the header as in the code below but it needs to be on the bottom of the page.
private void Page_Load(object sender, EventArgs e)
{
if (!Sitecore.Context.PageMode.IsPageEditor)
{
var javascriptRef = new LiteralControl("<script src=\"/js/vendor/jquery.bxslider.min.js\"></script>");
Page.Header.Controls.Add(javascriptRef);
}
}
How can I add an external script reference to the footer? I know how to add it to the header as in the code below but it needs to be on the bottom of the page.
private void Page_Load(object sender, EventArgs e)
{
if (!Sitecore.Context.PageMode.IsPageEditor)
{
var javascriptRef = new LiteralControl("<script src=\"/js/vendor/jquery.bxslider.min.js\"></script>");
Page.Header.Controls.Add(javascriptRef);
}
}
Share
Improve this question
edited Nov 27, 2013 at 15:22
Soner Gönül
98.9k103 gold badges220 silver badges373 bronze badges
asked Nov 27, 2013 at 15:20
Filip HuysmansFilip Huysmans
1,3412 gold badges21 silver badges46 bronze badges
3 Answers
Reset to default 5You can have in your aspx file next lines of code(add a Literal control just before the end of body tag) :
<asp:Literal runat="server" ID="litScript"/>
</body>
From code behind you have :
private void Page_Load(object sender, EventArgs e)
{
if (!Sitecore.Context.PageMode.IsPageEditor)
{
litScript.Text="<script src=\"/js/vendor/jquery.bxslider.min.js\"></script>";
}
}
For projects where individual controls/sublayouts control the scripts that they include on the page, I typically use Page.ClientScript
to manage script inclusions.
ClientScriptManager
has a number of useful methods for including scripts within a page, and will detect if a script has already been included by paring the types and script names so that scripts aren't included multiple times (could be nice for handling dependencies, but I prefer to use requirejs).
The three main script registration methods are:
RegisterClientScriptBlock
RegisterClientScriptInclude
RegisterStartupScript
RegisterClientScriptBlock
dumps the script at the top of the page
Page.ClientScript
.RegisterClientScriptBlock(
GetType(),
scriptName,
"<script>alert('do stuff');</script>");
RegisterClientScriptInclude
dumps <script src="">
at the top of the page
Page.ClientScript
.RegisterClientScriptInclude(
GetType(),
scriptName,
"path/to/scriptname.js");
RegisterStartupScript
dumps the script at the bottom of the page
Page.ClientScript
.RegisterStartupScript(
GetType(),
scriptName,
"<script>alert('do stuff');</script>");
Unfortunately there's no native RegisterStartupScriptInclude
which would allow for external scripts to be added to the bottom of the page, so I wrote an extension method:
public static class ClientScriptManagerExtensions
{
/// <summary>
/// Registers the startup script with the <see cref="Page"/> object using a type,
/// a key, and a url
/// </summary>
/// <param name="source">
/// The <see cref="ClientScriptManager"/> with which the script should be registered
/// </param>
/// <param name="type">The type of startup script to register</param>
/// <param name="key">The key of the startup script to register</param>
/// <param name="url">The url of the startup script include to register</param>
public static void RegisterStartupScriptInclude(this ClientScriptManager source,
Type type,
string key,
string url)
{
if (source == null)
throw new ArgumentNullException("source");
var script = string.Format(@"<script src=""{0}""></script>", HttpUtility.HtmlEncode(url));
source.RegisterStartupScript(type, key, script);
}
}
This allows developers to register external scripts to be executed at the close of the </form>
element, which tends to be close enough to </body>
for purposes of performance.
Page.ClientScript.RegisterStartupScriptInclude(Page.GetType(), "jquery", "path/to/jquery.js");
Page.ClientScript.RegisterStartupScriptInclude(GetType(), "mycontrol", "path/to/mycontrol.js");
The .Net aspx ultimately boils down to the structure of an html page. An html page by definition has a header and a body, no footer.
So if you have this definition in the .aspx
<html>
<head runat="server"/>
<body>[Some content]</body>
</html>
you can use
Page.Header.Controls.Add(javascriptRef);
In order to just inject it in the body, you can use
Page.Controls.Add(javascriptRef);
Or you can do what @sitecore climber suggested.
本文标签: javascriptC add external scripts in footer from code behind (Sitecore)Stack Overflow
版权声明:本文标题:javascript - C# add external scripts in footer from code behind (Sitecore) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741672201a2391670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论