I'm trying to add a javascript link to my Sharepoint page via a HttpModule, using the 'OnPreRequestHandlerExecute' event.
However nothing is happening and the script link is not added but i'm unsure why
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
HttpContext currentContext = HttpContext.Current;
Page page = currentContext.CurrentHandler as Page;
if (page != null)
{
AddScript(page);
}
}
void AddScript(Page page)
{
var link = new ScriptLink
{
Defer = true,
Localizable = false,
Name = "_LAYOUTS/CompanyFolder/AcceptCookies.js"
};
page.Controls.Add(link);
}
Update 1
I also tried registering the script and this did not work either (with and without the ResolveClientUrl
).
void AddScript(Page page)
{
if (!page.ClientScript.IsClientScriptIncludeRegistered(scriptName))
{
string script = "<script type=\"text/javascript\" src=\"/_layouts/CompanyFolder/AcceptCookies.js\"></script>";
page.ClientScript.RegisterClientScriptInclude(scriptName, page.ResolveClientUrl(script));
}
}