Add scripts to head dynamically - Web Dev And Stuff
Friday, August 08, 2008 11:14 AM djsolid

Add scripts to head dynamically

Many times when i develop a user control i have the need to add some javascript to the header of the page. But the header doesn't have a method where you can add script and RegisterStartupScript inserts the script at the bottom of the page.

So i thought let's create an extension method...

This is how this method can be used for inserting javascript tags to a page's header...

Without the script tags....

And not only javascript but custom css files, meta tags etc...

That's it! Javascript, css files, meta tags and anything else can be inserted to the header dynamically!!

Enjoy...

 

kick it on DotNetKicks.com Filed under: , , , , ,

Comments

# Add scripts to head dynamically

Friday, August 08, 2008 4:22 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# carnewsservice.info » Add scripts to head dynamically

Friday, August 08, 2008 5:36 AM by carnewsservice.info » Add scripts to head dynamically

Pingback from  carnewsservice.info » Add scripts to head dynamically

# re: Add scripts to head dynamically

Friday, August 08, 2008 8:29 AM by ILog

Hi,

I also used such a way but found one serious limitation. When my Page contains own script with <%= AnotherControl.UniqueID %> inside and I try to register another script from UserControl, the function throws an error. Could you, please, confirm that it works for you in the described situation? Thanks.

# re: Add scripts to head dynamically

Friday, August 08, 2008 9:25 AM by Bryan Migliorisi

Since your function does scripts, styles, meta tags and anything else that goes in to the head, why not rename your function to a generic name - public void Insert(this HtmlHead h,string TextToInsert)

Alternatively, a solution I developed was to create a custom control and place it in the head.  I exposed a number of methods and placed the user control inside of my master page.  On each page I simply find the control in the page parent and then do the appropriate manipulations.  Same idea, just a different implementation.

# re: Add scripts to head dynamically

Friday, August 08, 2008 11:45 AM by jstengel

# links for 2008-08-08 [delicious.com] &laquo; dstelow notes&#8230;

Pingback from  links for 2008-08-08 [delicious.com] &laquo; dstelow notes&#8230;

# re: Add scripts to head dynamically

Sunday, August 10, 2008 4:41 AM by djsolid

@ ILog : I see no reason why the above wouldn't work on a situation like that.

@ Bryan : You are right. A different name would be more appropriate. But this is just an example[:P].

Your idea is pretty nice but it would be a little tricky if you had a master page nested in an anohter master page and inside it you had a usercontol with a nested user control. Sounds rare but that was a situation i was facing. But still a great idea! Thanks for sharing!

@jstengel : Nice blog post! This is something that anyone must consider before start adding scripts and styles to the header of the page dynamically! Thanks for mentioning!

# re: Add scripts to head dynamically

Sunday, August 10, 2008 5:38 AM by Mike.Borozdin

Excellent idea!

# re: Add scripts to head dynamically

Tuesday, March 03, 2009 4:25 PM by ...

Interessante Informationen.

# re: Add scripts to head dynamically

Wednesday, March 04, 2009 4:58 AM by ...

Gute Arbeit hier! Gute Inhalte.

# re: Add scripts to head dynamically

Sunday, March 15, 2009 8:10 AM by ...

Sehr wertvolle Informationen! Empfehlen!

# re: Add scripts to head dynamically

Saturday, November 07, 2009 6:43 AM by CleverCat

There are a method in Page that does this:

RegisterStartupScript(scriptID, script);

# re: Add scripts to head dynamically

Thursday, November 12, 2009 11:00 AM by Tristan

Thanks a lot. This was a great help :)

# re: Add scripts to head dynamically

Thursday, April 29, 2010 12:16 PM by shailesh patel

Another good example of using extension method.

# re: Add scripts to head dynamically

Wednesday, June 02, 2010 5:06 AM by abdulwakeel

I want to fetch all redios channels how can

www.islamabadid.com/.../fm-100

Implement in this page

# re: Add scripts to head dynamically

Wednesday, May 18, 2011 2:00 AM by weblogs.asp.net

Add scripts to head dynamically.. Not so bad :)

Leave a Comment

(required) 
(required) 
(optional)
(required)