/// <summary> /// Includes the js. /// </summary> /// /// <param name="path">The path.</param> /// <param name="type">(Optional) The type.</param> /// <param name="head">(Optional) if place into page head.</param> /// <param name="placeholder">(Optional) The placeholder.</param> /// <param name="id">(Optional) The identifier.</param> public void IncludeJs(string path, Type type = null, bool head = false, string placeholder = null, string id = null) { Page page = _httpContext.GetCurrentHandler(); if (page == null) { return; } //GENERATE CONTROL ID IF APPLICABLE if (string.IsNullOrWhiteSpace(id)) { id = GenerateSafeName(path); } //RESOLVE URL IF APPLICABLE if (type != null) { //USE EMBEDDED RESOURCE path = page.ClientScript.GetWebResourceUrl(type, path); } else { //HANDLE ANY STRANGE CHARACTERS AND RESOLVE path = WebUtility.HtmlEncode(path.StartsWith("~/") ? page.ResolveUrl(path) : path); } //ADD SCRIPT IF APPLICABLE if (!head && string.IsNullOrWhiteSpace(placeholder)) { page.ClientScript.RegisterStartupScript(page.GetType(), id, ToJsLink(path), false); return; } Control control; if (head) { //HANDLE PLACEMENT OF STYLESHEETS IF AVAILABLE control = !string.IsNullOrWhiteSpace(placeholder) ? (page.Header.FindControl(placeholder) ?? page.Header) : page.Header; } else { //GET PLACEMENT OF SCRIPTS IF AVAILABLE control = page.FindControl(placeholder); } //DETERMINE IF CONTROL AVAILABLE AND SCRIPT NOT ADDED BEFORE if (control != null && control.FindControl(id) == null) { var jslink = new HtmlGenericControl("script"); jslink.ID = id; jslink.Attributes.Add("src", path); //ADD STYLESHEET TO PAGE HEAD control.Controls.Add(jslink); } }
/// <summary> /// Adds the script reference. /// </summary> /// /// <param name="html">The HTML.</param> /// <param name="reference">The reference.</param> public static void AddScriptReference(this HtmlHelper html, ScriptRef reference) { Page page = _httpContext.GetCurrentHandler(); if (page != null) { PageManager.ConfigureScriptManager(page, reference); } }
/// <summary> /// Gets the web resource URL. /// </summary> /// <param name="resourceName">Name of the resource.</param> /// <param name="type">(Optional) The type.</param> /// <returns> /// The web resource URL. /// </returns> public static string GetWebResourceUrl(string resourceName, Type type = null) { Page page = _httpContext.GetCurrentHandler() ?? CachedPage; //DETERMINE ASSEMBLY IF APPLICABLE if (type == null) { //HANDLE IF COMING FROM TELERIK RESOURCE ASSEMBLY if (resourceName.StartsWith("Telerik.Sitefinity.Resources.")) { type = typeof(Reference); } else { //USE LOCAL RESOURCE BY DEFAULT type = typeof(PageHelper); } } return(page != null?page.ClientScript.GetWebResourceUrl(type, resourceName) : string.Empty); }