我见过几个论坛,人们提到他们不能在DNN中为JavaScript文件使用Defer和Async。这是真的?请参阅下面的两个链接:
Link 1
Link 2
是否有任何新的发展或最近的更新将允许这样做而不必使用DNN的“性能”标签中启用的“复合”功能?
有人提到您可以在$(document).ready()函数中包含JavaScript函数。
我还想知道下面的方法1和2在DNN中包含JavaScript文件有什么区别?
方法1(ASP)
var moduleName = "MiniCart";
//Include js file, prefix these files with this Type's name to avoid conflicts with multiple modules on the same page
System.Web.UI.HtmlControls.HtmlGenericControl scriptInclude = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.Header.FindControl(ID);
if (scriptInclude == null)
{
scriptInclude = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
scriptInclude.Attributes["type"] = "text/javascript";
scriptInclude.Attributes["src"] = this.TemplateSourceDirectory + "/Scripts/toastr.js";
scriptInclude.ID = ID + $"{moduleName}_Toastr";
Page.Header.Controls.Add(scriptInclude);
}
}
方法2(客户端资源管理器)
ClientResourceManager.RegisterScript(this.Page, "~/DesktopModules/MiniCart/Scripts/miniCartAjaxLoader.js", FileOrder.Js.DefaultPriority, "DnnFormBottomProvider");
方法3
我知道您还可以创建JavaScript库扩展并将其像普通模块一样安装,并在后面的代码中引用所需的库名称,如下所示:
JavaScript.RequestRegistration("Fancybox");
JavaScript.Register(this.Page);
GitHub上已经有一些库可用于DNN:https://github.com/EngageSoftware/DNN-JavaScript-Libraries
方法4
您还可以使用dnnJsInclude将JavaScript文件添加到文档中:
http://www.dnnsoftware.com/wiki/client-resource-management-api
http://bdukes.github.io/Making-Full-Use-of-the-Client-Resource-Management-Framework/#/code-registration
最佳答案
从DNN 9.2.0开始,您可以使用HtmlAttributesAsString
属性来指定Client Dependency Framework本机不支持的属性。例如HtmlAttributesAsString="defer:'defer'"
或HtmlAttributesAsString="async:'async'"
。
这也适用于诸如跨域和完整性属性之类的事情,例如
<dnn:DnnJsInclude ID="DnnJsBootStrap" runat="server" FilePath="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" ForceProvider="DnnFormBottomProvider" Name="bootstrap" Version="4.1.1" HtmlAttributesAsString="crossorigin:'anonymous',integrity:'sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T'" />
输出:
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js?cdv=142" crossorigin="anonymous" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" type="text/javascript"></script>
见https://github.com/Shazwazza/ClientDependency/wiki/Html-Attributes
关于javascript - 在DNN中包含JavaScript文件的不同方法以及如何使用Defer和Async加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50859995/