Copied to clipboard

Flag this post as spam?

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


  • Mike Manusama 45 posts 195 karma points
    Jul 14, 2016 @ 19:09
    Mike Manusama
    0

    Add CSS/JS files with checkbox

    Hi all,

    I am working on a large site that will use different JS libraries and different CSS files on given pages. The general stylesheets have been added to the master template, but to reduce HTTP requests, I would like to add certain JS libraries and stylesheets to pages without having them load on every page.

    For instance, I have some pages that have forms, and others that don't. There are specific JS libraries and CSS files I would like to add on pages that have forms that the other pages do not need.

    In order to make the user experience easier for content editors in our company, I think it would be best to just have a data type on my document type that has a list of css files and a list of js files with checkboxes next to them. The content editor could just check the files they will need to add to the pages as they see fit.

    I am pretty sure I explained this correctly, and I hope there is an answer for this. Searching the forums, I did not see an answer for this. Is this even considered the best practice or are there better ways you guys can think of to achieve the selective css/js calls? Thanks in advance!

  • Ian 178 posts 752 karma points
    Jul 14, 2016 @ 20:06
    Ian
    0

    I understand your desire to limit assets being loaded on pages and I too think that's a good way to go. Personally i'm not sure if giving the user options to select which assets to include is the right approach but it may be for you in limited circumstances. We instead tend to load assets based on the items being used on a page. For example if a particular grid is being used the grid view may include require js or css. If we've done an accordion using nested content we will load assets in only when needed.

    We do this using the ClientDependency Module and syntax like this in our views

    Html.RequiresCss("~/css/accordion.css");
    Html.RequiresCss("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css");
    

    This however hasn't worked for us in partial views so there scripts are loaded in the standard way.

Please Sign in or register to post replies

Write your reply to:

Draft