Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



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:

[code:c#]
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);
[/code]

Add some JavaScript inside the page header:

[code:c#]
HtmlGenericControl Include2 = new HtmlGenericControl("script");
Include2.Attributes.Add("type", "text/javascript");
Include2.InnerHtml = "alert('JavaScript in Page Header');";
this.Page.Header.Controls.Add(Include2);
[/code]

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.

Currently rated 4.6 by 10 people

  • Currently 4.599999/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: General
Posted by crpietschmann on Saturday, June 03, 2006 12:38 AM
Permalink | Comments (9) | Post RSSRSS comment feed

Related posts

Comments

טיסות לניו יורק

Monday, July 24, 2006 6:36 AM

טיסות לניו יורק

nice post

SimoneB

Saturday, July 29, 2006 7:24 AM

SimoneB

Nice tip. Thanks for sharing.

Chris Pietschmann

Monday, August 07, 2006 10:26 AM

Chris Pietschmann

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

ביטוח רכב

Friday, November 10, 2006 3:03 AM

ביטוח רכב

it's limited in the fact that you cannot use it to place JavaScript inside the Head tags of the Page

Holger

Monday, January 29, 2007 9:11 AM

Holger

hello from germany,
nice tipp!
is there a chance to register a javascript using the clientscriptmanager to the bottom of of the page?



Andrew

Wednesday, April 04, 2007 8:38 AM

Andrew

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

Andrew

Wednesday, April 04, 2007 9:29 AM

Andrew

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

Thursday, April 05, 2007 3:43 PM

Vadim

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

Friday, May 18, 2007 9:41 AM

Kasim

Thanks for the post!

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann