Add Javascript inside head element of page from code behind
Howto: Add JavaScript inside Head element from code behind
Here is the sample how you can do that.
1) Add some JavaScript inside page header
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl si = new System.Web.UI.HtmlControls.HtmlGenericControl();
si.TagName ="script";
si.Attributes.Add("type",@"text\javascript");
si.InnerText = @"alert('I am in Head Element.')";
this.Page.Header.Controls.Add(si);
}2) Add .js file reference inside page header
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl si = new System.Web.UI.HtmlControls.HtmlGenericControl();
si.TagName ="script";
si.Attributes.Add("type",@"text\javascript");
si.Attributes.Add("src","util.js");
this.Page.Header.Controls.Add(si);
}
Revision number 1, Tuesday, May 25, 2010 8:31:02 AM by shailesh patel
This is not the most up to date version of this article. The most recent version can be found here.
You must Login to comment.