1

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);          
    }
}

3 Answers 3

5

You 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>";

   }
}
Sign up to request clarification or add additional context in comments.

Comments

5

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 comparing 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");

1 Comment

This is a fantastic answer
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.