ASP.NET 2.0: Place JavaScript inside the Page.Header
The Page.ClientScript object allows you to place JavaScript inside the Page, but it’s limited in the fact that you cannot use it to place JavaScript inside the Head tags of the Page. But fortunately there is a way to do this.
Add a .js file include inside the page header:
HtmlGenericControl Include = new HtmlGenericControl("script");
Include.Attributes.Add("type", "text/javascript");
Include.Attributes.Add("src", "http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js");
this.Page.Header.Controls.Add(Include);
Add some JavaScript inside the page header:
HtmlGenericControl Include2 = new HtmlGenericControl("script");
Include2.Attributes.Add("type", "text/javascript");
Include2.InnerHtml = "alert('JavaScript in Page Header');";
this.Page.Header.Controls.Add(Include2);
It would be nice if there was a Page.Header.ClientScript object that you could use to place JavaScript within the header of the Page, but for now we’ll just have to use the method stated above.
Related Posts
-
How to Hide a Column in AG-Grid and Prevent it from Displaying in the Tool Panel
11 Oct 2023 -
JavaScript: Loop through Array (using forEach)
03 Oct 2023 -
JavaScript: Parse a String to a Date
28 Sep 2023 -
JavaScript: Format Date to String
28 Sep 2023 -
JavaScript: How to Convert Map to an Array of Objects
28 Sep 2023 -
Check if element exists using JavaScript or jQuery
25 Sep 2023