Add an 'async' attribute to a JS include t

2019-07-11 21:33发布

问题:

I saw how it can be done in MVC, but for a specific project I'm using Web-Forms.

I have

<asp:PlaceHolder ID="PlaceHolder1" runat="server">
    <%: System.Web.Optimization.Scripts.Render("~/file.js") %>
</asp:PlaceHolder>

which generates the html:

<script src="/file.js?v=5jui8vHEj5_iAkaGiAccPPb2LcsdK1jQ1"></script>

I wish to add the sync attribute to the above tag.

I'd appreciate some guidance. Thanks.

回答1:

You can use RenderFormat to achieve this:

 <%: System.Web.Optimization.Scripts.RenderFormat ("<script src='{0}' async ></script>", "~/file.js") %>

Renders as: <script src="/file.js" async=""></script>



回答2:

This method would should the magic:

 <script src='<%: System.Web.Optimization.Scripts.Render("~/file.js") %>' async> </script>

I extract from:

Bundling and Minification