Rachit's Blog (Moved)
Moved to http://weblogs.asp.net/rachit

Adding Javascript file link in Master Page

Sunday, January 14, 2007 9:58 PM
Adding a referece of Javascript files in Master Pages is a pain. Because the location of Javascript can be in directory, you can simply write this:

<script type="text/javascript" src="../Scripts/some.js />
 
This will NOT work as when you deploy your site, it would depend on the directory structure.
Another problem is you also can NOT do the following:
 
<script type="text/javascript" src='<% = ResolveUrl("~/Scripts/some.js") %>' />

because Master Page won't allow any server-side tags.
The ultimate solution to this I found, is, as follows:

protected void Page_Load(object sender, EventArgs e)
{
  Page.ClientScript.RegisterClientScriptInclude("somescript", ResolveUrl("some.js"));
}


which produces the result

<script src="/Test1/some.js" type="text/javascript" />
 
Perfect! If you have a better solution, please add in the comments section. I'm definitely interested.

 

Update (01/28/2007):

Simon drew my attention that my technique above will add the JavaScript file to the < BODY > section of the page. In some cases, you don't want that. Here is my solution to add Javascript file to the < HEAD / > part. Thanks Simon.

HtmlGenericControl myJs = new HtmlGenericControl();
myJs.TagName = "script";
myJs.Attributes.Add("type", "text/javascript");
myJs.Attributes.Add("language", "javascript"); //don't need it usually but for cross browser.
myJs.Attributes.Add("src", ResolveUrl("some.js"));
this.Page.Header.Controls.Add(myJs);

Feedback

# re: Adding Javascript file link in Master Page

Just had the same problem myself. Although your solution works I would prefer my script to be included in the head of the document and not in the body and am yet to find a way of achieving this. 1/28/2007 6:15 AM | Simon Dingley

# re: Adding Javascript file link in Master Page

Hi Simon,
Great question...didn't think about when I posted the thread. Here's the solution:

HtmlGenericControl myJs = new HtmlGenericControl();
myJs.TagName = "script";
myJs.Attributes.Add("type", "text/javascript");
myJs.Attributes.Add("language", "javascript"); //don't need it usually but for cross browser.
myJs.Attributes.Add("src", ResolveUrl("some.js"));
this.Page.Header.Controls.Add(myJs);

This will add Javascript to the <HEAD /> part. I've updated the original thread. 1/28/2007 9:07 AM | Rachit

# re: Adding Javascript file link in Master Page

Works a treat - thanks Rachit. 2/5/2007 1:57 PM | Simon

# re: Adding Javascript file link in Master Page

Is this applicable for .NET Framework 1.1?? 2/22/2007 3:15 AM | Mark

# re: Adding Javascript file link in Master Page

Mark, I think there's HTMLGenericControl in ASP.Net 1.1 (not 100% sure though), so it should work as well...give it a try! ;) 2/23/2007 8:30 AM | Rachit

# re: Adding Javascript file link in Master Page

Hi i have tried using the following code which is a direct translation of your C# but i get an HTTP Exception error on the last line of code!

Partial Class MasterPages_MasterPage

Inherits System.Web.UI.MasterPage

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Dim myJs As HtmlGenericControl = New HtmlGenericControl

myJs.TagName = "script"

myJs.Attributes.Add(

"type", "text/javascript")
myJs.Attributes.Add(

"language", "javascript")
myJs.Attributes.Add(

"src", ResolveUrl("javascript1.js"))

Me.Page.Header.Controls.Add(myJs)

End Sub
End

Class

do you have to add a library or anything else? have i made a mistake?

Cheers

Tuppers 3/1/2007 5:03 AM | Tuppers

# re: Adding Javascript file link in Master Page

Hi Tuppers,
I don't know much about the syntax of vb.net so I used this: http://www.kamalpatel.net/ConvertCSharp2VB.aspx and code looks similar to yours...don't know what else is missing.

Dim myJs As HtmlGenericControl = New HtmlGenericControl()
myJs.TagName = "script"
myJs.Attributes.Add("type", "text/javascript")
myJs.Attributes.Add("language", "javascript")
myJs.Attributes.Add("src", ResolveUrl("some.js"))
Me.Page.Header.Controls.Add(myJs) 3/1/2007 6:06 AM | Rachit

# re: Adding Javascript file link in Master Page

thanks for the reply i will try your code in a c# code behind file! see if that works!

What you have posted is what i have just seems to have added some spacing in it!

Cheers again will get back to you!

Tuppers 3/1/2007 8:30 AM | Tuppers

# re: Adding Javascript file link in Master Page

Hi,
I just added the following code for linking javascript in the master page
<script src="/Test1/some.js" type="text/javascript" /> and this works fine. 2/23/2008 11:34 AM | LS

# re: Adding Javascript file link in Master Page

Hello,
can anyone plz give me an example to use javascript in asp.net files.
Thankx Jackson 3/18/2008 7:20 AM | Jackson Coutinho

# re: Adding Javascript file link in Master Page

Great stuff!

This has saved me from going crazy.

Thanks,
Neil. 4/11/2008 5:57 PM | Neil

Post a comment





 

Please add 5 and 3 and type the answer here: