473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
Nov 17 '05 #1
3 3907
ni*****@talk21.com (Nikki) wrote in
news:8d**************************@posting.google.c om:
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/
Nov 17 '05 #2
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!
Nov 17 '05 #3
Of course! Many thanks Chris.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Matt Kruse | last post by:
Since this topic has come up several times in other threads, I thought I'd make a separate thread and gather opinions from (hopefully) a more varied range of newsgroup participants. What are...
6
by: Jamie | last post by:
Hi there, I have a bit of java script which I would like to include in all of my html pages. Is there an easy way to include it with a reference or something. I don't want to have to copy...
21
by: matvdl | last post by:
I have a system that was originally developed in asp - the pages are saved in SQL (there are over 10,000 pages) and saved to a temp directory in the server when requested by a client. I have...
7
by: Matt Jensen | last post by:
Howdy I want to simulate with .Net what I used to do with classic ASP where you would have a series of include files with utility functions that you would include when you needed them. I read...
2
by: Ramon Rocha | last post by:
Dear friends, How do I do to includes files .aspx in ASP.NET pages <script></script>? Ex: config.aspx ------------ Dim strLanguage as string strLanguage = "en-US"
14
by: Julesh | last post by:
Hello, I am new to ASP and am trying to make some changes to some ASP 3.0 code I have inherited. I have a number of ASP pages with VBS as the base language, on each of these pages I have...
1
by: Nemisis | last post by:
Hi everyone, In a asp.net web application, where is the best way/best practice way to store external javascript files that i want to reference in pages/ web controls etc? None, of my scripts...
1
by: mtek | last post by:
Does Javascript support include files? I have some functions that I need in 4 pages with Javascript. Can I somehow include that data by using a general include file? All of the Javascript is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.