private static int GetScriptPriority(ScriptInjectOrder scriptPosition) { switch (scriptPosition) { case ScriptInjectOrder.a_BeforejQuery: return(4); case ScriptInjectOrder.b_BeforeDnnXml: return(14); case ScriptInjectOrder.c_BeforeDomPositioning: return(29); case ScriptInjectOrder.d_BeforeDnnControls: return(39); default: return(100); } }
/// <summary> /// Inject a js Library reference into the page /// </summary> /// <param name="page">The page object of the page to add the script reference to</param> /// <param name="name">Unique name for the script</param> /// <param name="lib">Url to the script library (can be relative/absolute)</param> /// <param name="inHeader">True if to go in the page header, false if to go into the page body</param> /// <param name="scriptPosition">Enumerated position for calculating where to place the script. Works for DNN 6.1 and later only, ignored in earlier versions</param> public static void InjectJsLibrary(System.Web.UI.Page page, string name, string lib, bool inHeader, ScriptInjectOrder scriptPosition) { var major = default(int); var minor = default(int); var build = default(int); var revision = default(int); var allowInHeader = false; var useDotNetNukeWebClient = false; var dnnWebClientOk = false; if (DNNUtilities.SafeDNNVersion(major, minor, revision, build)) { switch (major) { case 4: case 5: allowInHeader = true; break; default: //6.0 and above if (minor >= 1) { //6.1 and abpve if (revision < 1) { //6.1.0 - work with change in order that means no placement of scripts in header allowInHeader = false; useDotNetNukeWebClient = true; } else { //6.1.1 and above - use client dependency framework useDotNetNukeWebClient = true; } } else { //6.0 allowInHeader = true; } break; } } if (useDotNetNukeWebClient) { //use the dotnetnuke web client methods var priority = GetScriptPriority(scriptPosition); //get the imbibe type var imbibe = Type.GetType("DotNetNuke.Web.Client.ClientResourceManagement.ClientResourceManager, DotNetNuke.Web.Client"); if (imbibe != null) { //create arrays of both types and values for the parameters, in readiness for the reflection call var paramTypes = new Type[4]; var paramValues = new object[4]; paramTypes[0] = typeof(System.Web.UI.Page); paramValues[0] = page; paramTypes[1] = typeof(string); paramValues[1] = lib; paramTypes[2] = typeof(int); paramValues[2] = priority; paramTypes[3] = typeof(string); if (inHeader && allowInHeader) { paramValues[3] = "PageHeaderProvider"; } else { paramValues[3] = "DnnBodyProvider"; } //call the method to register the script via reflection var registerScriptMethod = imbibe.GetMethod("RegisterScript", paramTypes); if (registerScriptMethod != null) { registerScriptMethod.Invoke(null, paramValues); //worked OK dnnWebClientOk = true; } } } if (!useDotNetNukeWebClient || dnnWebClientOk == false) { //earlier versions or failed with reflection call, inject manually if (inHeader && allowInHeader) { if (page.Header.FindControl(name) == null) { var jsLib = new System.Web.UI.HtmlControls.HtmlGenericControl("script"); jsLib.Attributes.Add("src", lib); jsLib.Attributes.Add("type", "text/javascript"); jsLib.ID = name; page.Header.Controls.Add(jsLib); } } else { //register a script block - doesn't go in the header if (page.ClientScript != null) { page.ClientScript.RegisterClientScriptInclude(name, lib); } } } }