/// <summary>
        /// Returns a fully qualified script tag for loading ww.jquery.js
        /// </summary>
        /// <param name="jQueryLoadMode"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string wwJqueryLink(jQueryLoadModes jQueryLoadMode = jQueryLoadModes.Default, string url = null)
        {
            if (jQueryLoadMode == jQueryLoadModes.Default)
            {
                jQueryLoadMode = ScriptLoader.jQueryLoadMode;
            }
            if (jQueryLoadMode == jQueryLoadModes.None)
            {
                return(url ?? string.Empty);
            }

            string wwJQueryUrl    = string.Empty;
            string fallbackScript = string.Empty;

            var script = ClientScriptProxy.Current;

            if (!string.IsNullOrEmpty(url))
            {
                wwJQueryUrl = WebUtils.ResolveUrl(url);
            }
            else if (jQueryLoadMode == jQueryLoadModes.WebResource ||
                     jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)
            {
                wwJQueryUrl = script.GetClientScriptResourceUrl(typeof(WebResources),
                                                                WebResources.WWJQUERY_SCRIPT_RESOURCE);
            }

            if (string.IsNullOrEmpty(wwJQueryUrl))
            {
                wwJQueryUrl = WebUtils.ResolveUrl("~/scripts/ww.jquery.min.js");
            }

            return("<script src=\"" + wwJQueryUrl + "\" type=\"text/javascript\"></script>");
        }
        /// <summary>
        /// Inserts a script link to load jQuery into the page based on the jQueryLoadModes settings
        /// of this class. Default load is by CDN plus WebResource fallback
        /// </summary>
        /// <param name="url">
        /// An optional explicit URL to load jQuery from. Url is resolved.
        /// When specified no fallback is applied
        /// </param>
        /// <returns>full script tag and fallback script for jQuery to load</returns>
        public static string jQueryLink(jQueryLoadModes jQueryLoadMode = jQueryLoadModes.Default, string url = null)
        {
            if (jQueryLoadMode == jQueryLoadModes.Default)
            {
                jQueryLoadMode = ScriptLoader.jQueryLoadMode;
            }
            if (jQueryLoadMode == jQueryLoadModes.None)
            {
                return(string.Empty);
            }

            string jQueryUrl      = string.Empty;
            string fallbackScript = string.Empty;

            var script = ClientScriptProxy.Current;

            if (!string.IsNullOrEmpty(url))
            {
                jQueryUrl = WebUtils.ResolveUrl(url);
            }

            // not available anymore
            if (jQueryLoadMode == jQueryLoadModes.WebResource)
            {
                jQueryLoadMode = jQueryLoadModes.ContentDeliveryNetwork;
            }

            else if (jQueryLoadMode == jQueryLoadModes.WebResource)
            {
                jQueryUrl = script.GetClientScriptResourceUrl(typeof(WebResources),
                                                              WebResources.JQUERY_SCRIPT_RESOURCE);
            }
            else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)
            {
                jQueryUrl = ScriptLoader.jQueryCdnUrl;

                if (!string.IsNullOrEmpty(jQueryCdnUrl))
                {
                    // check if jquery loaded - if it didn't we're not online and use WebResource
                    fallbackScript =

                        @"<script type=""text/javascript"">if (typeof(jQuery) == 'undefined')
        document.write(unescape(""%3Cscript src='{0}' type='text/javascript'%3E%3C/script%3E""));
</script>";

                    fallbackScript = string.Format(fallbackScript, WebUtils.ResolveUrl(jQueryCdnFallbackUrl));
                }
            }

            string output = "<script src=\"" + jQueryUrl + "\" type=\"text/javascript\"></script>";

            // add in the CDN fallback script code
            if (!string.IsNullOrEmpty(fallbackScript))
            {
                output += "\r\n" + fallbackScript + "\r\n";
            }

            return(output);
        }
        /// <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;

            // we no longer deliver jQuery in package
            if (jQueryLoadMode == jQueryLoadModes.WebResource)
            {
                jQueryLoadMode = jQueryLoadModes.ContentDeliveryNetwork;
            }

            if (!string.IsNullOrEmpty(jQueryUrl))
            {
                p.RegisterClientScriptInclude(control, typeof(WebResources), jQueryUrl, ScriptRenderModes.HeaderTop);
            }
            else if (jQueryLoadMode == jQueryLoadModes.WebResource)
            {
                p.RegisterClientScriptResource(control, typeof(WebResources), WebResources.JQUERY_SCRIPT_RESOURCE, ScriptRenderModes.HeaderTop);
            }
            else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)
            {
                // Load from CDN Url specified
                p.RegisterClientScriptInclude(control, typeof(WebResources), 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(WebResources),
                                                         WebResources.JQUERY_SCRIPT_RESOURCE);
                p.RegisterClientScriptBlock(control, typeof(WebResources),
                                            "jquery_register", string.Format(scriptCheck, jQueryUrl), true,
                                            ScriptRenderModes.HeaderTop);
            }

            return;
        }
Example #4
0
 /// <summary>
 /// Injects a full jQuery script link and CDN fallback (if using CDN) into the page.
 /// Version information and CDN Urls are based on the static settings in the <see cref="System.Web.ControlResources"/> class.
 /// </summary>
 /// <param name="mode">Optional: Determines where jQuery is loaded from (CDN, WebResources, Script)</param>        
 /// <param name="url">Optional url from where to load jQuery</param>
 public static string jQueryLink( jQueryLoadModes jQueryLoadMode = jQueryLoadModes.Default, string url = null )
 {
     return ScriptLoader.jQueryLink(jQueryLoadMode,url);
 }
        /// <summary>
        /// Returns a fully qualified script tag for loading ww.jquery.js
        /// </summary>
        /// <param name="jQueryLoadMode"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string wwJqueryLink(jQueryLoadModes jQueryLoadMode = jQueryLoadModes.Default, string url = null)
        {
            if (jQueryLoadMode == jQueryLoadModes.Default)
                jQueryLoadMode = ScriptLoader.jQueryLoadMode;
            if (jQueryLoadMode == jQueryLoadModes.None)
                return url ?? string.Empty;

            string wwJQueryUrl = string.Empty;
            string fallbackScript = string.Empty;

            var script = ClientScriptProxy.Current;

            if (!string.IsNullOrEmpty(url))
                wwJQueryUrl = WebUtils.ResolveUrl(url);
            else if (jQueryLoadMode == jQueryLoadModes.WebResource ||
                     jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)

                wwJQueryUrl = script.GetClientScriptResourceUrl(typeof(WebResources),
                                                          WebResources.WWJQUERY_SCRIPT_RESOURCE);

            if (string.IsNullOrEmpty(wwJQueryUrl))
                wwJQueryUrl = WebUtils.ResolveUrl("~/scripts/ww.jquery.min.js");

            return "<script src=\"" + wwJQueryUrl + "\" type=\"text/javascript\"></script>";
        }
        /// <summary>
        /// Inserts a script link to load jQuery into the page based on the jQueryLoadModes settings
        /// of this class. Default load is by CDN plus WebResource fallback
        /// </summary>
        /// <param name="url">
        /// An optional explicit URL to load jQuery from. Url is resolved. 
        /// When specified no fallback is applied
        /// </param>        
        /// <returns>full script tag and fallback script for jQuery to load</returns>
        public static string jQueryUiLink(jQueryLoadModes jQueryLoadMode = jQueryLoadModes.Default, string url = null)
        {
            if (jQueryLoadMode == jQueryLoadModes.Default)
                jQueryLoadMode = ScriptLoader.jQueryLoadMode;
            if (jQueryLoadMode == jQueryLoadModes.None)
                return url ?? string.Empty;

            string jQueryUiUrl = string.Empty;
            string fallbackScript = string.Empty;

            var script = ClientScriptProxy.Current;

            if (!string.IsNullOrEmpty(url))
                jQueryUiUrl = WebUtils.ResolveUrl(url);
            else if (jQueryLoadMode == jQueryLoadModes.WebResource)
                jQueryUiUrl = WebUtils.ResolveUrl(jQueryUiLocalFallbackUrl);
            else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)
            {
                jQueryUiUrl = ScriptLoader.jQueryUiCdnUrl;

                if (!string.IsNullOrEmpty(jQueryCdnUrl))
                {
                    // check if jquery loaded - if it didn't we're not online and use WebResource
                    fallbackScript =
            @"<script type=""text/javascript"">if (typeof(jQuery) == 'undefined')
            document.write(unescape(""%3Cscript src='{0}' type='text/javascript'%3E%3C/script%3E""));
            </script>";

                    fallbackScript = string.Format(fallbackScript, WebUtils.ResolveUrl(jQueryUiLocalFallbackUrl));
                }
            }

            string output = "<script src=\"" + jQueryUiUrl + "\" type=\"text/javascript\"></script>";

            // add in the CDN fallback script code
            if (!string.IsNullOrEmpty(fallbackScript))
                output += "\r\n" + fallbackScript + "\r\n";

            return output;
        }