[tips ‘n’ tricks] Storing javascript code in an external file using asp net web forms

Pubblicato: novembre 15, 2012 in ASP.NET, Best Practice, HTML, Script, Tips and tricks
Tag:, , , , , , , ,

ASPNET Tips 'n' TricksIt’s a best practice storing scripts like js, or css, in an external file, the only exception to this rule is for lightweight script specific for a page like the following example:

$(function() {
alert('do something');

in the other cases the external file is the best choice for scalability, maintainability, degradability of your code. Asp.Net Web Forms is not supports the special grapheme for including external file .js using tag <script></script>.

The Visual Studio compilator not signs this syntax like an error or like a warning:
<script type=”text/javascript” src=”~/Scripts/jquery-1.4.1.min.js”></script>

But running our appliaction on Cassini we have this run-time error: $ undefined.

Javascript ettor

How can we solve this problem?

We have many ways to solve the problem with advantages and disadvantages.

Solution nr. 1 : using absolute path

Using syntax: <script src=”/Scripts/jquery-1.4.1.min.js” type=”text/javascript”></script> the script is include correctly.

This are the advantages:

  1. Standard HTML code
  2. Easy solution

This is the disadvantage:

  1. If the root of your application is different when will deploy it, the problem will not to be resolve.

Solution nr. 2: resolve the script with ResolveUrl

ResolveUrl is a method (of namespace System.Web.UI), supported by .Net Framework 1.0,  for converting a URL into one that is usable on the requesting client. We can solve, therefore, our problem with follow syntax:

<script src='<%=  ResolveUrl(“~/Scripts/jquery-1.4.1.min.js”) %>’ type=”text/javascript”></script>

the advantages of this choice are:

  1. easy solution
  2. solution indipendent by root of application

the disadvantage is that the solution is not a standard and performances of the page decrease because we are using server function inside a client declaration.

Solution nr. 3 : using ASP-NET Ajax

ScriptManager is the first aspnet control inside a web page. For MSDN It manages ASP.NET Ajax script libraries and script files, partial-page rendering, and client proxy class generation for Web and application services. We can use it for all application that we have written with MS Framework 3.0+ based on Asp.Net Ajax technology.

Using ScriptManager control we can solve our problem with this markup:

<form runat="server">
<asp:ScriptManager ID="sm1" runat="server">
<asp:ScriptReference Path="~/Scripts/jquery-1.4.1.min.js" />
<script type="text/javascript">
$(function () {
alert('Do something');


It’s a best practice (only for internet applications)  add commons libraries, like jQuery core, from a web repository. With ASP.NET 4.0 we can do this with simple two steps.

1. Insert this code into the section Application_Start of Global.asax.cs

void Application_Start(object sender, EventArgs e)
// map a simple name to a path
ScriptManager.ScriptResourceMapping.AddDefinition("jQuery", new ScriptResourceDefinition
Path = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
DebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"


2. Change the script manager mark up with this declaration:

<form runat="server">
<asp:ScriptManager ID="sm1" runat="server">
<asp:ScriptReference Name="jQuery" />
<script type="text/javascript">
$(function () {
alert('Do something');

the advantage of this solution is the easy implementation on asp.net ajax web application. The disadvantage is that the ScriptManager load many libraries and than the page will be heavier than simple HTML page.

The CDN solution not working if your application is an intranet asp net project with some with limitations in web surfing.

rien ne va plus and happy coding  =)

  1. Heya i am for the first time here. I came across this board and I to
    find It truly useful & it helped me out a lot. I am hoping to present one thing back
    and help others such as you helped me.


Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )


Connessione a %s...