Click here to Skip to main content
Click here to Skip to main content

How to add javascript or stylesheet to header of page?

, 9 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will show you how to dynamically add javascript or stylesheet to the header of page.


Some times you want to dynamically add a javascript to the header of page or even dynamically add a stylesheet to yuor page.


When we are using Page.ClientScript functions, the scripts won't be added to the header it is going to be added to different part of page.

Using the code

The following code shows how you can dynamically add script or stylesheet or any other type of control to the header of page.

	'Add the css to header
        Dim hControl As LiteralControl = New LiteralControl
        hControl.Text = "<link href=""default.css"" type=""text/css""  
        rel=""stylesheet"" />"
        'Add javascript for the header
        Dim header As LiteralControl = New LiteralControl
        header.Text = "<script type=""text/javascript""   

Here is the new version of my code which i got it from comments (nice job Matteo) In the following code you can avoid using literal: To add a meta tag:

HtmlMeta metadescription = new HtmlMeta();
metadescription.Name = "description";
metadescription.Content = "Your page description here";
To add a stylesheet:
HtmlLink css = new HtmlLink(); 
css.Href = "mystyle.css";
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
To add an external javascript reference:
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "mylibrary.js";
Thanks again Matteo;)

Points of Interest

As you can see first we define a LiteralControl and then we assign our javascript or stylesheet to the text properties of LiteralControl and then we will add the LiteralControl to header of the page. Now when you run the page, you will see the javascript or stylesheet has been added to the header. you can call put all this code in afunctiona nd call it on the page_load of page.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Armin Kabir
Software Developer (Senior) Innovative Design
United States United States
No Biography provided

Comments and Discussions

AnswerPage.ClientScript.RegisterClientScriptInclude can also be used rather than HtmlGenericControl Pinmembersatishgv198527-Nov-12 3:26 
QuestionPage.Header was nothing PinmemberMember 819032824-Oct-12 0:09 
SuggestionDo something when javascript is loaded PinmemberMember 14778521-Jul-11 5:41 
QuestionInsert js file BEFORE other javascript code in the head block...? Pinmemberkupps23-Jun-10 13:41 
AnswerRe: Insert js file BEFORE other javascript code in the head block...? Pinmemberaaron.barker1-Jan-11 11:39 
AnswerRe: Insert js file BEFORE other javascript code in the head block...? Pinmembersatishgv198527-Nov-12 3:28 
GeneralMaster Pages PinmemberMember 30555644-Nov-08 3:09 
GeneralRe: Master Pages PinmemberJohn Hutton13-Jan-09 11:21 
GeneralAvoid Literal PinmemberMatteo Casati9-Apr-08 6:58 
GeneralRe: Avoid Literal PinmemberArmin Kabir9-Apr-08 7:56 
GeneralRe: Avoid Literal PinmemberMatteo Casati9-Apr-08 8:03 
GeneralRe: Avoid Literal PinmemberArmin Kabir9-Apr-08 8:53 
GeneralRe: Avoid Literal PinmemberAndreyMir10-Apr-08 9:07 
GeneralRe: Avoid Literal PinmemberNisha Agrawal12-Aug-09 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 9 Apr 2008
Article Copyright 2008 by Armin Kabir
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid