views:

207

answers:

5

Hi there,

I have around 40 aspx pages in my website. I want to use a javascript function which needs to be called when any of the 40 pages is loaded. something like

I could have this function in the "head" section of each of the 40 aspx pages and then call in the body onload event. But I would like to have this function at a single place. This is an existing app so I cannot create a master page and have all the pages derive from it.

Any ideas?

+1  A: 

You can create a base class that it will be inherited by all these pages and write the logic of inserting javascript there.

So, do something like it:

Create a base class:

[Serializable]
public class RequiresFunctionBasePage : System.Web.UI.Page
{
    public RequiresFunctionBasePage()
    {
     this.Load+= new delegate
     {
      this.ClientScript.RegisterClientScriptInclude("yourScript", "http://yoursite.com/yourJs.js");

            this.ClientScript.RegisterStartupScript(this.GetType(),
                    "functionOnload", "functionName();", true);
     }
    } 
}

And into your aspx codebehind:

public partial class yourPageNameGoesHere : RequiresFunctionBasePage 
{
(...)
Cleiton
I would like to stay away from making changes to the code-behind files(aspx.vb)
AgentHunt
AgentHunt, no, you wont need to make a big change in your code-behind, only you have to do it is to create a base class and inherit your aspx from that. I've edited my post to show you how you would archive that.
Cleiton
+5  A: 

You will need to put the function in a .js file and call from your pages, but you will need to link to the script in all your 40 pages, since you can't add master page.

Amr ElGarhy
A: 

You will need to modify the masterpage to include a new JS file with your new function or place your JS function in one of the JS files already included in the masterpage.

Armitage
Is there any way I can have some sort of js/htm file under wwwroot and just reference this file in the aspx pages, such that when the page is loaded the javascript function in the htm file is automatically invoked.
AgentHunt
You certainly can. Just drag the script from solution explorer onto each of the 40 aspx pages.
Rob
A: 

I suppose if you wanted to make it difficult/elegant, you could make an HttpModule that injects the script. That way it's only in one place and you can wire it up in the web.config.

Here's a sample httpModule

Public Class JavascriptInjector
Implements IHttpModule



Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
    AddHandler context.PreRequestHandlerExecute, AddressOf PreRequestHandlerExecute
End Sub

Private Sub PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)

    Dim myPage = TryCast(HttpContext.Current.CurrentHandler, Page)
    If myPage Is Nothing Then Exit Sub
    AddHandler myPage.InitComplete, AddressOf Page_Init

End Sub

Sub Page_Init()
    Dim myPage = TryCast(HttpContext.Current.CurrentHandler, Page)
    If myPage Is Nothing Then Exit Sub
    Dim path = myPage.ResolveUrl("~/js/jscript.js")
    myPage.ClientScript.RegisterClientScriptInclude(myPage.GetType, "common", path)
End Sub

Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

End Sub

End Class

Here's an entry in the web.config

<httpModules> <add name="javascriptInjector" type="JavascriptInjector"/> </httpModules>

Rob
Sounds like a good approach..Any clues on how to go about it? Would it still involve making changes to all aspx pages?
AgentHunt
I'll try this out and let you know :)
AgentHunt
Would adding coding like this be functionally equivalent to calling the javascript function in body onLoad event of the aspx page?Example:<html><body onload="alert('Hello')"></html>Should I use the RegisterStartupScript instead?
AgentHunt
Once you have your hooks in the page events, you can do whatever works. RegisterStartupScript should work just fine. Note the boolean argument at the end to optionally wrap you script in <script> tags
Rob
This works beautifully. Thank you!!!Can you give me any ideas..if I want to have server side code to check if the client browser supports cookies(if not, redirect to a custom page), can I have that in the same HttpModule or would having a HttpHandler be a better option. And in what event would that checkk go into?Again thanks a lott!
AgentHunt
A: 

hello,

For a fully client side solution you could create a javascript file (possibly named script.js) that you link to from the head of your master page. In that file put your function.

So, the javascript in the script.js file would be something like this:

function SampleFunction(text) {
  alert(text);
// or whatever code you want
}

and then in the head of your pages

<script type="text/javascript" src="script.js"></script>

and then your onload can be

onload="SampleFunction('hi there');"

have fun :)

robb

Robb C