How do you centrally store and include reusable javascript functions into .aspx pages?

Discussion in 'ASP .Net' started by Nikki, Jul 1, 2003.

  1. Nikki

    Nikki Guest

    Hi,

    I would like to create a central storage place for javascript
    functions that I can then access from any of my asp.net projects.

    An example of what I need it for is as follows:

    I have a class library that contains a function I use all the time (to
    create a link around some text). That function in turn calls a
    javascript function. Usually I would have put the javascript on the
    page by using:

    If (Not IsClientScriptBlockRegistered("clientScript")) Then
    RegisterClientScriptBlock("clientScript", strJava)
    End If

    (where strJava = my javascript function etc)

    Because IsClientScriptBlockRegistered is part of the Page class, I
    can't do this in my class library.

    The other option I thought of was including a javascript file as an
    include on my .aspx page, but I would want the .js page to use in many
    different projects, not just in one project.

    Does anyone have any ideas as to what is the best way to store
    reusuable javascript functions and how to access them?

    Many thanks,

    Nikki
    Nikki, Jul 1, 2003
    #1
    1. Advertising

  2. (Nikki) wrote in
    news::

    > Hi,
    >
    > I would like to create a central storage place for javascript
    > functions that I can then access from any of my asp.net
    > projects.
    >
    > An example of what I need it for is as follows:
    >
    > I have a class library that contains a function I use all the
    > time (to create a link around some text). That function in turn
    > calls a javascript function. Usually I would have put the
    > javascript on the page by using:
    >
    > If (Not IsClientScriptBlockRegistered("clientScript")) Then
    > RegisterClientScriptBlock("clientScript", strJava)
    > End If
    >
    > (where strJava = my javascript function etc)
    >
    > Because IsClientScriptBlockRegistered is part of the Page class,
    > I can't do this in my class library.


    Nikki,

    Yes you can. Create a method that takes a System.Web.UI.Page as a
    parameter. Here's an example in C#:

    public class JavaScriptHelpers
    {
    public static void RegisterJavaScriptSource(
    System.Web.UI.Page page,
    string key,
    string code)
    {
    if (!page.IsClientScriptBlockRegistered(key))
    page.RegisterClientScriptBlock(key, code);
    }
    }


    From your page, you can call this method like this:

    // Use "Me" instead of "this" as the first parameter
    // in VB.NET.
    JavaScriptHelpers.RegisterJavaScriptSource(this,
    "clientScript", strJava);


    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
    Chris R. Timmons, Jul 1, 2003
    #2
    1. Advertising

  3. Nikki

    Nikki NZ Guest

    Hi Chris,

    Thanks for your prompt reply!

    I have tried passing in the Page to the function as follows, but it
    didn't like it.

    Public Class Detail

    Function MapText(ByVal PC As String, ByVal Page As System.Web.UI.Page)
    As String

    ...

    End Function
    End Class

    It says that System.Web.UI.Page is not defined. I think this is what you
    meant in your reply.

    Many thanks in advance for your help.

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Nikki NZ, Jul 1, 2003
    #3
  4. Nikki

    Nikki Guest

    Of course! Many thanks Chris.
    Nikki, Jul 2, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny

    Reusable functions

    Danny, Jan 15, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    568
    Danny
    Jan 15, 2004
  2. Mike Hutton
    Replies:
    1
    Views:
    408
    Ian Suttle
    Nov 11, 2004
  3. Toby Inkster

    Reusable form functions

    Toby Inkster, Nov 12, 2005, in forum: HTML
    Replies:
    0
    Views:
    436
    Toby Inkster
    Nov 12, 2005
  4. RadioSignal

    passing variables from .aspx.cs code pages to .aspx pages

    RadioSignal, Sep 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    2,190
    Mirronelli
    Oct 22, 2005
  5. Wayne Stuart

    Newbie query: Centrally set variables?

    Wayne Stuart, Mar 18, 2007, in forum: Javascript
    Replies:
    2
    Views:
    121
    Wayne Stuart
    Mar 18, 2007
Loading...

Share This Page