问题
I'm using DotNetNuke 4.8.x and want to utilize jQuery. Could anyone possibly suggest me on what is the best way to integrate jQuery into DNN? I won't be able to upgrade DNN version to 5 which has built-in support for jQuery.
Your advice would be much appreciated.
回答1:
To avoid loading jQuery multiple times, it might be best to use a client-side script, like the one given here.
In the server-side, you can load jQuery library in the page header during the Page.Init or Page.Load event:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
Page.Header.Controls.Add(...)
End Sub
You may want to use a helper method to create the HtmlGenericControl, like
Public Function HeadScriptResource(ByVal src As String) As HtmlGenericControl
Dim Include As New HtmlGenericControl("script")
Include.Attributes.Add("type", "text/javascript")
Include.Attributes.Add("src", src)
Return Include
End Function
This way, you can add any script to header using the source path as parameter:
Page.Header.Controls.Add(HeadScriptResource("/resources/shared/scripts/jquery/jquery.min.js"))
DNN 4.9.1 and above are shipped with jQuery located in
/resources/shared/scripts/jquery/jquery.min.js
If every server you need has web access, which is not evident in corporate environments, you can use hosted jQuery, for example: http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
With hosted jQuery, you have to use the https url if the site uses https. Otherwise, you'll get browser warnings. It may be easier to always use https.
If you need to add jQuery on page-by-page basis, you can also use Page Header Tag in Page Settings as suggested here.
See also:
- How to get Page.ClientScript.RegisterClientScriptInclude to include in the head?
- jQuery Tips for DotNetNuke Developers
来源:https://stackoverflow.com/questions/3534111/what-is-the-best-way-to-include-jquery-in-dotnetnuke-4-8-x