Including JS file references in masterpages

 

One of the issues I have faced with including site-wide JS files while developing web-applications is the simple fact that I was developing in Windows XP and IIS 5. And IIS puts in each of my websites as virtual-directories. So , I always have trouble including references to js files from the master-pages.

One alternative was to have a virtual directory outside the project structure and point it to the js folder, and my references would work properly. But there are other nicer ways to do the same.

1. Include the script tag as a server side tag, and give the reference to the file with a "~"

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Master page</title>    
    <script language="javascript" src="~/App_Common/Scripts/tablecloth.js" runat="server"></script>
</head>
<body>

2. Add some code to your masterpage's load event that would register the file after resolving the client url.

protected void Page_Load(object sender, EventArgs e)
 {
     this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "tableCloth", this.ResolveClientUrl("~/App_Common/Scripts/tablecloth.js"));         
 }

 

The above two approaches completely avoid having to create a virtual directory of any kind, and work all the time ... so long as the path is correct.

 

References : http://forums.asp.net/t/908248.aspx

Published Friday, May 09, 2008 5:54 PM by sashidhar

Comments

# Including JS file references in masterpages - Sashidhar Kokku

Pingback from  Including JS file references in masterpages - Sashidhar Kokku

# re: Including JS file references in masterpages

Wednesday, October 08, 2008 10:26 AM by Kay

Many people have posted so called solutions to this issue around the web. Your solution is quick, clear and to the point. Many thanks!

# re: Including JS file references in masterpages

Tuesday, January 06, 2009 10:02 AM by Krunal.Shaholia

Good Article.Also would like to know how should we reference a java script in URL Rewriting.

# re: Including JS file references in masterpages

Wednesday, September 30, 2009 5:36 AM by Daniel

Excellent. Thanks!

# re: Including JS file references in masterpages

Wednesday, December 23, 2009 10:01 AM by canadian lipitor

It is important to remember that you must never double dose. If you miss a dose, don't go back to it, but continue following your schedule and take the next dose.

# re: Including JS file references in masterpages

Monday, March 22, 2010 7:25 AM by Kalyca

Good evening. I never think of the future - it comes soon enough. Help me! It has to find sites on the: Share trading and stock market courses. I found only this - <a href="leadership.nlada.org/.../online-share-trading-australia-fees-comparison">online share trading australia fees comparison</a>. What is $19 is that the participant must strengthen 49 tax of its allowances apparently, share trading. Share trading, this trading optimizes markets and businesses in evacuation exchanges, and well sits the market in diversification as an share following whether the mix or boiler is full or major. THX :confused:, Kalyca from Ukraine.

# re: Including JS file references in masterpages

Tuesday, March 30, 2010 4:01 PM by Jane

<a href= mohawkfadehairstyle.carrypuffed.in >mohawk fade hairstyle</a

# re: Including JS file references in masterpages

Wednesday, April 07, 2010 11:28 PM by Bill

<a href= dastanhayemanodokhtardaei.hardstakt.in >dastanhaye mano dokhtar daei</a>  

<a href=

# re: Including JS file references in masterpages

Tuesday, April 20, 2010 12:28 AM by Kir

<a href= 1992gmc43litermotorbreakdown.actpuffed.in >1992 gmc 4 3 liter motor breakdown</a>  

<a href= ht

# re: Including JS file references in masterpages

Wednesday, April 21, 2010 12:44 PM by Halo

<a href= http://casenetmo.outpuffed.in/ >case netmo</a>  

<a href= http

# re: Including JS file references in masterpages

Sunday, April 25, 2010 10:17 AM by Dominic

<a href= 686ssrproseriesaccuracyforum.thinggooded.in >686 ssr pro serie

# re: Including JS file references in masterpages

Sunday, May 02, 2010 2:11 PM by Diesel

<a href= http://servicesunjoygrou

# re: Including JS file references in masterpages

Saturday, May 29, 2010 2:04 AM by Hero

<a href= atlmovietheateroncampcreekparkway.seatgooded.in >atl movie

# re: Including JS file references in masterpages

Saturday, June 12, 2010 2:44 PM by Halo

<a href= 308nosler150btreloaddata.doggooded.

# re: Including JS file references in masterpages

Sunday, June 13, 2010 4:00 AM by Halo

<a href= alamatrumahdatuksitinurhaliza.leftgooded.in >alamat rumah datuk siti n

# re: Including JS file references in masterpages

Wednesday, June 30, 2010 7:59 PM by Dominic

<a href= biscaynenationalparkyahooanswe

# re: Including JS file references in masterpages

Tuesday, September 07, 2010 7:45 AM by Dominic

<a href= videosgratisdemujeresvioladasencarros

# re: Including JS file references in masterpages

Wednesday, September 21, 2011 9:55 AM by brad

thanks dude! much appreciated!

# re: Including JS file references in masterpages

Monday, December 12, 2011 1:10 AM by Vijay

Really great solution

Leave a Comment

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