Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steve 140 posts 321 karma points
    Apr 11, 2016 @ 05:19
    Steve
    0

    Add JS to end of <body> from a partial view

    Hi - I'd like to add specific jQuery code to the end of the body tag of my main layout, but not for every page template. In this case it is code for a revolution slider on the homepage, but not needed on other sub pages.
    Not sure if this is possible... but can't figure out how to do it!
    Thanks for any assistance.
    Steve

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 11, 2016 @ 06:02
    Sebastiaan Janssen
    103

    In your main layout add the following where you want the JS code to be inserted:

    @RenderSection("Scripts", false)
    

    On each of the views where you want to add some scripts to the page, you can now implement that section:

    @section Scripts {
            <script type="text/javascript" src="/js/myscript.js"></script>
    }
    
  • Steve 140 posts 321 karma points
    Apr 11, 2016 @ 14:44
    Steve
    1

    Sebastiaan
    Works like a charm.
    Thanks for answering for what I now understand is not specific to Umbraco but rather a general razor/MVC question.
    Once understood, I also found this post at stack overflow very helpful for integrating with partial views.
    Steve

Please Sign in or register to post replies

Write your reply to:

Draft