AgentHunt
AgentHunt

Reputation: 669

Single javascript function for multiple web pages

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?

Upvotes: 0

Views: 1165

Answers (5)

Amr Elgarhy
Amr Elgarhy

Reputation: 68912

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.

Upvotes: 5

Robb C
Robb C

Reputation: 152

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

Upvotes: 0

Rob
Rob

Reputation: 1328

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>

Upvotes: 0

Cleiton
Cleiton

Reputation: 18113

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 
{
(...)

Upvotes: 1

Andrew Magill
Andrew Magill

Reputation: 2510

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.

Upvotes: 0

Related Questions