ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

AddJavaScript

Dynamically adds a javascript file (.js) to a page even if using master page.

Source

public static void AddJavaScript(this Page page, string url)
{
    HtmlGenericControl js = new HtmlGenericControl("script");
    js.Attributes["type"] = "text/javascript";
    js.Attributes["src"] = url;
    page.Header.Controls.Add(js);
}

Example

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.AddJavaScript("jsFile.js");
    }
}

Author: Paulo Castilho

Submitted on: 1 jun. 2010

Language: C#

Type: System.Web.UI.Page

Views: 5790