How can I use the script defer attribute for ASP MVC 4 Bundles with Scripts.Render

后端 未结 4 1942
太阳男子
太阳男子 2021-02-01 04:18

I have looked through Google and Stackoverflow and haven\'t found an answer for this. Is there any built in way to make a bundle execute as deffered or does someone know of an e

4条回答
  •  渐次进展
    2021-02-01 04:38

    Late answer but I would like to add because I am across almost similar problem where I need to add a data attribute to the bundled scripts (in relation with a GDPR cookie solution).

    The solution CookieBot was blocking some of the scripts on Auto-Mode, therefore, I need to implement in Manual mode and mark all of my scripts to be ignored by CookieBot script.

    My scripts in BungleConfig.cs looks like this:

                  bundles.Add(new StyleBundle("~/Bundles/late").Include(
                      "~/Scripts/jquery.pep.js",
                      "~/Scripts/jQuery.easing.1.3.js",
                      "~/Content/swiper/js/swiper.js",
                      "~/Scripts/uiActions.js",
                      "~/Scripts/xxm.js",
                      "~/Scripts/addtohomescreen.js",
                      "~/Scripts/d3.min.js"
                ));
    

    And, in my _Layout.csthml.

      @Scripts.RenderFormat("", "~/Bundles/late")
    

    And, this is what I got after being rendered.

    
    

    Now, the original question, one can add defer or async along with or without data attribute.

    Scripts.Render and scripts.RenderFormat both are referenced to System.Web.Optimization, and should be already availabe on MVC project.

提交回复
热议问题