ASP.NET 2.0: Place JavaScript inside the Page.Header

3. June 2006

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.

asp.net, JavaScript

Comments

7/24/2006 6:36:00 PM #
nice post
SimoneB
SimoneB
7/29/2006 7:24:00 PM #
Nice tip. Thanks for sharing.
8/7/2006 10:26:00 PM #
SimoneB has posted an enhancement to my code posted above. He has created a HeadScriptManager helper class to make it easier to add js and css to the page header.


dotnetslackers.com/.../247.aspx">HeadScriptManager helper class
11/10/2006 3:03:00 PM #
it's limited in the fact that you cannot use it to place JavaScript inside the Head tags of the Page
1/29/2007 9:11:00 PM #
hello from germany,
nice tipp!
is there a chance to register a javascript using the clientscriptmanager to the bottom of of the page?



4/4/2007 8:38:00 PM #
I know it's probably not the best solution, but I combined a couple techniques to get my JavaScript tag to appear (close to) the bottom of the page.

I added a placeholder right above the close form tag:
[aspTonglaceHolder ID="plhJavaScript" runat="server"][/aspTonglaceHolder]

(replaced HTML tags with brackets)...got ASP.NET yellow screen error, lol.

Then dynamically created the path to the javascript file so that it will work in subdirectories too:

      'create generic html control
      Dim JS As New HtmlControls.HtmlGenericControl("script")
      JS.Attributes.Add("type", "text/javascript")
      JS.Attributes.Add("src", HttpContext.Current.Request.ApplicationPath & "/js/ScriptFile.js")
      plhJavaScript.Controls.Add(JS)

I hope the formatting comes out ok...

Cheers,
Andrew
4/4/2007 9:29:00 PM #
Ok, I guess that formatting didn't come out very well on my last post, LOL.

That last code had a problem with Event Validation anyway (Invalid postback or callback argument).  Giving the generic control an ID seems to have fixed the problem for now...

I'm going to post the solution to http://blog.killfly.com/ where I can control the formatting ;)

Chris, you can delete or edit that last post if you want since it's kind of ugly.

-Andrew

Vadim
Vadim
4/6/2007 3:43:00 AM #
Hi all, the tip is realy helpfull.
But could you help me out with following:

I have a MasterPage and script that builds menu with CSS. How can I use it?

Thanks Vadim
Kasim
Kasim
5/18/2007 9:41:00 PM #
Thanks for the post!