ASP.NET Trick – How to reference javascript file from the ASP.NET master page?

First thing first, why would you reference the JavaScript file from the master page? Well, the best reason for this is whenever you have a application wide javascript, you would like to refer it from one code base instead of adding a reference to all the individual pages. Adding JavaScript reference through master page adds lot more complexities espcially runtime path execution.

Here is the code you need to put in your master page Page_Load event to reference JavaScript file called ApplicationScript.js. Once you have this in the master page, it is available to the all the ASP.NET web forms using this master page.

protected void Page_Load(object sender, EventArgs e)
// Add the Javacript in the master page
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), “ApplicationJavaScript”, this.ResolveClientUrl(“~/Scripts/ApplicationScript.js”));

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s