Here's how I implemented what you are doing without a HttpModule. I didn't like the httpmodule idea because if I forgot to register it and it wasn't running, my app wouldn't work and it would have been a non obvious bug. The page absolutely needs the JS included so I decided to put it in the base Page class of the project.
I implemented this years ago, and wanted to be able to include more than just scripts, it supports css, meta tags, etc.... I forget why I used Page.Header.Controls.Add instead of just Page.ClientScript.RegisterClientScriptInclude but there was a reason.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
namespace YourNamespace
{
public class HeaderIncludesManager
{
private List<string> m_IncludedFiles = new List<string>();
public void IncludeScript(string s)
{
IncludeScript(s, null);
}
public bool IsIncluded(string file)
{
return (m_IncludedFiles.Find(s => s.Equals(file, StringComparison.InvariantCultureIgnoreCase)) != null);
}
public void IncludeScript(string script, string condition)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (!IsIncluded(script) || page == null)
return;
string scriptFile = string.Format("/{0}/{1}?v={2}", MyConfig.JSDir, script, MyConfig.BuildNumber);
if (page.Header != null)
{
string scriptTag = String.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>\n", scriptFile);
if (!String.IsNullOrEmpty(condition))
scriptTag = String.Format("<!--[{0}]><script language=\"javascript\" type=\"text/javascript\" src=\"{1}\"></script><![endif]-->\n", condition, scriptFile);
page.Header.Controls.Add(new LiteralControl(scriptTag));
m_IncludedFiles.Add(script);
}
else if (!page.ClientScript.IsClientScriptIncludeRegistered(GetType(), scriptFile))
{
page.ClientScript.RegisterClientScriptInclude(GetType(), scriptFile, scriptFile);
m_IncludedFiles.Add(script);
}
}
public void IncludeCss(string css)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (!IsIncluded(css) || page == null)
return;
string cssfile = string.Format("/{0}/{1}?v={2}", MyConfig.CssDir, css, MyConfig.BuildNumber);
if (page.Header != null)
{
((Page)HttpContext.Current.CurrentHandler).Header.Controls.Add(new LiteralControl(String.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />\n", cssfile)));
m_IncludedFiles.Add(css);
}
}
public void IncludeJQuery()
{
IncludeScript("jquery-1.2.3.min.js");
}
public void IncludeJQueryUI()
{
IncludeJQuery();
IncludeScript("jquery.ui.1.0.min.js");
IncludeCss("jquery-theme.css");
}
public void IncludeFlotScripts()
{
IncludeJQuery();
IncludeScript("flot/jquery.flot.js");
IncludeScript("flot/excanvas.pack.js", "if IE");
}
}
public class MyPage : Page
{
public HeaderIncludesManager HeaderIncludes = new HeaderIncludesManager();
}
public class MyControl : UserControl
{
public new MyPage Page
{
get
{
return (MyPage)base.Page;
}
}
}
public class SomeControlThatNeedsScripts : MyControl
{
protected override void OnLoad(EventArgs e)
{
Page.HeaderIncludes.IncludeJQueryUI();
base.OnLoad(e);
}
}
}