/// <summary> /// Loads jQuery depending on configuration settings (CDN, WebResource or site url) /// and injects the full script link into the page. /// </summary> /// <param name="control"></param> /// <param name="jQueryUrl">Optional url to jQuery as a virtual or absolute server path</param> public static void LoadjQuery(Control control, string jQueryUrl) { ClientScriptProxy p = ClientScriptProxy.Current; if (!string.IsNullOrEmpty(jQueryUrl)) { p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryUrl, ScriptRenderModes.HeaderTop); } else if (jQueryLoadMode == jQueryLoadModes.WebResource) { p.RegisterClientScriptResource(control, typeof(ControlResources), ControlResources.JQUERY_SCRIPT_RESOURCE, ScriptRenderModes.HeaderTop); } else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork) { // Load from CDN Url specified p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryCdnUrl, ScriptRenderModes.HeaderTop); // check if jquery loaded - if it didn't we're not online and use WebResource string scriptCheck = @"if (typeof(jQuery) == 'undefined') document.write(unescape(""%3Cscript src='{0}' type='text/javascript'%3E%3C/script%3E""));"; jQueryUrl = p.GetClientScriptResourceUrl(control, typeof(ControlResources), ControlResources.JQUERY_SCRIPT_RESOURCE); p.RegisterClientScriptBlock(control, typeof(ControlResources), "jquery_register", string.Format(scriptCheck, jQueryUrl), true, ScriptRenderModes.HeaderTop); } return; }
/// <summary> /// Loads the appropriate jScript library out of the scripts directory and /// injects into a WebForms page. /// </summary> /// <param name="control"></param> /// <param name="jQueryUiUrl">Optional url to jQuery as a virtual or absolute server path</param> public static void LoadjQueryUi(Control control, string jQueryUiUrl) { ClientScriptProxy p = ClientScriptProxy.Current; // jQuery UI isn't provided as a Web Resource so default to a fixed URL if (jQueryLoadMode == jQueryLoadModes.WebResource) { //throw new InvalidOperationException(Resources.WebResourceNotAvailableForJQueryUI); jQueryUiUrl = UrlUtils.ResolveUrl(jQueryUiLocalFallbackUrl); } if (!string.IsNullOrEmpty(jQueryUiUrl)) { p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryUiUrl, ScriptRenderModes.Header); } else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork) { // Load from CDN Url specified p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryUiCdnUrl, ScriptRenderModes.Header); // check if jquery loaded - if it didn't we're not online and use WebResource string scriptCheck = @"if (typeof(jQuery.ui) == 'undefined') document.write(unescape(""%3Cscript src='{0}' type='text/javascript'%3E%3C/script%3E""));"; p.RegisterClientScriptBlock(control, typeof(ControlResources), "jquery_ui", string.Format(scriptCheck, UrlUtils.ResolveUrl(jQueryUiLocalFallbackUrl)), true, ScriptRenderModes.Header); } return; }
/// <summary> /// Code that embeds related resources (.js and css) /// </summary> /// <param name="scriptProxy"></param> protected void RegisterResources(ClientScriptProxy scriptProxy) { scriptProxy.LoadControlScript(this, jQueryJs, ControlResources.JQUERY_SCRIPT_RESOURCE, ScriptRenderModes.HeaderTop); scriptProxy.RegisterClientScriptInclude(Page, typeof(ControlResources), CalendarJs, ScriptRenderModes.Header); string cssPath = CalendarCss; if (!string.IsNullOrEmpty(Theme)) { cssPath = cssPath.Replace("/base/", "/" + Theme + "/"); } scriptProxy.RegisterCssLink(Page, typeof(ControlResources), cssPath, cssPath); }
/// <summary> /// Registers an individual script item in the page /// </summary> /// <param name="script"></param> private void RegisterScriptItem(ScriptItem script) { // Allow inheriting from control which is the default behavior if (script.RenderMode == ScriptRenderModes.Inherit) { script.RenderMode = RenderMode; } // Skip over inline scripts if (script.RenderMode == ScriptRenderModes.Inline) { return; } // special case jquery load from Content network if (ScriptLoader.jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork && script.Resource == ControlResources.JQUERY_SCRIPT_RESOURCE || script.Resource == "jquery" || script.Resource == "jquery.js" || script.Src.ToLower().Contains("/jquery.js")) { ScriptLoader.LoadjQuery(Page); return; } // special case jquery-uiload from Content network if (script.Src.ToLower().Contains("/jquery-ui.") || script.Resource == "jqueryui" || script.Resource == "jquery-ui") { ScriptLoader.LoadjQueryUi(Page, null); return; } string src = string.Empty; Type ctlType = typeof(ControlResources); if (script.ResourceControlType != null || !string.IsNullOrEmpty(script.ResourceControl)) { if (script.ResourceControlType != null) { ctlType = script.ResourceControlType; } else { Control ctl = UrlUtils.FindControlRecursive(Page, script.ResourceControl, false); if (ctl != null) { ctlType = ctl.GetType(); } else { throw new ArgumentException("Invalid Web Control passed for resource retrieval. Please pass a control from the assembly where the resource are located."); } } src = scriptProxy.GetClientScriptResourceUrl(Page, ctlType, script.Resource); } else if (!string.IsNullOrEmpty(script.Resource)) { src = scriptProxy.GetClientScriptResourceUrl(this, ctlType, script.Resource); } else { src = ResolveUrl(script.Src); // Fix up the URL so we can allow ~/script syntax if (script.AllowMinScript && !HttpContext.Current.IsDebuggingEnabled) { src = src.ToLower().Replace(".js", MinScriptExtension); } } // if there's a version number implied add a par if (!string.IsNullOrEmpty(script.Version)) { if (src.Contains("?")) { src += "&ver=" + script.Version; } else { src += "?ver=" + script.Version; } } scriptProxy.RegisterClientScriptInclude(Page, ctlType, src, script.RenderMode); }
/// <summary> /// Code that embeds related resources (.js and css) /// </summary> /// <param name="scriptProxy"></param> protected void RegisterResources(ClientScriptProxy scriptProxy) { scriptProxy.LoadControlScript(this, jQueryJs, WebResources.JQUERY_SCRIPT_RESOURCE, ScriptRenderModes.HeaderTop); scriptProxy.RegisterClientScriptInclude(Page, typeof(WebResources), CalendarJs, ScriptRenderModes.Header); string cssPath = CalendarCss; if (!string.IsNullOrEmpty(Theme)) cssPath = cssPath.Replace("/base/", "/" + Theme + "/"); scriptProxy.RegisterCssLink(Page, typeof(WebResources), cssPath, cssPath); }