Copied to clipboard

Flag this post as spam?

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

  • apload-gmbh 7 posts 27 karma points
    1 week ago

    JavaScript libraries only in master.cshtml

    Hi everyone

    I noticed that a JS-Library didn't work, when I put it directly in a template of a documenttype which is rendered somewhere in <body>. I know it is better to put it in the <head> or just before the closing , but sometimes it was the best to put a script like the following just beside it, where it's needed.

    <script src="lightgallery.js"></script>
    <div id="lightgallery">

    In Umbraco 8 it worked very well. Now with Umbraco 9 I had to put `

    <script src="lightgallery.js"></script>

    ` into the master.cshtml and had to make an if-statement that it doesn't load on every page.

    Is that really the only way to make it work?


  • Huw Reddick 716 posts 2404 karma points
    1 week ago
    Huw Reddick

    if thes are view templates, you could define a secion in your master template which you then use in your view templates to add extra scripts.

    Example, in master template

        @await RenderSectionAsync("ExtraScripts", false)

    Then in your View template you can do the following

    @section ExtraScripts
        <script type="text/javascript" src="/scripts/vantage.ecommerce.cart.js"></script>    

    So now when your view loads, it's js include will get rendered correctly.

    Note, this only works in a View, it will not work for partial views. If you need to include js in a partial view then you can create an extension to do so.

Please Sign in or register to post replies

Write your reply to: