473,763 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reusing a script

I have a function sililar to the one below on one of my pages. It puts
the value of a field on a form into a variable called FieldName:

function checkSomething( ) {
var FieldName = document.frmFor mName.FieldName .value
}

I'd like to reuse the function on a different page (instead of writing
another almost identical script on that page), but I need to change the
name of the field in the script depending on the page that the script is
used on.

How do I change the script so that I can put the script in an includes
file and then pass the name of the field to the script according to the
page that the script is being used?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #1
2 1365
Luis wrote:
I have a function sililar to the one below on one of my pages. It puts
the value of a field on a form into a variable called FieldName:

function checkSomething( ) {
var FieldName = document.frmFor mName.FieldName .value
}

I'd like to reuse the function on a different page (instead of writing
another almost identical script on that page), but I need to change the
name of the field in the script depending on the page that the script is
used on.

How do I change the script so that I can put the script in an includes
file and then pass the name of the field to the script according to the
page that the script is being used?


You can pass a reference to something rather than hard code the
reference. Below some code that is fairly self explainatory.

You could also pass the name or id of something as a string, then use
something like:

function checkSomething( x) {
var theThing = document.getEle mentById(x);
....

... onclick="checkS omething('theFo rm');"....
Cheers, Fred,

It shows how to pass a reference to 'this',

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Re use Script</title>
<script type="text/javascript">
function checkSomething( a) {
var msg = 'You passed: ' + a;
if (a == '') msg += 'an empty string';
if (a.nodeName) msg += '\n Node name: ' +a.nodeName;
if (a.type) msg += '\n Type: ' + a.type;
if (a.id) msg += '\n ID: ' + a.id;
if (a.name) msg += '\n NAME: ' + a.name;
if (a.value) msg += '\n VALUE: ' + a.value;
alert(msg);
}
</script>
</head>
<body>
<div id="aDiv" onclick="checkS omething(this); "
style="padding: 10px 30px 10px 30px; background-color: #99cccc;
width: 200px; height 40px;">A clickable Div</div>
<form action="" name="theForm">
<input type="text" name="aTextInpu t" size="50"><br>
<input type="button" name="aButton" value="Pass the form"
onclick="checkS omething(this.f orm);">
<input type="button" name="aButton" value="Pass the 'text' input"
onclick="checkS omething(this.f orm.aTextInput) ;">
<input type="button" name="aButton"
value="Pass the 'text' input value"
onclick="checkS omething(this.f orm.aTextInput. value);">
<input type="reset" value="Reset">
</form>
</body>
</html>
Jul 23 '05 #2
JRS: In article <41************ **********@news .newsgroups.ws> , dated
Wed, 20 Oct 2004 06:58:15, seen in news:comp.lang. javascript, Luis
<an**********@w ebmail.co.za> posted :

How do I change the script so that I can put the script in an includes
file and then pass the name of the field to the script according to the
page that the script is being used?


Reading <URL:http://www.merlyn.demo n.co.uk/js-nclds.htm> should answer
most of your needs.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

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

Similar topics

2
9306
by: Jo Voordeckers | last post by:
Hello all, I'm pretty new to the Java newsgroups so I apologize for dropping this into several maybe offtopic groups. I'm sorry! So on to my problem... I've come to a point in our RMI application where I need to have server callbacks to the client RMI applications. I've used the technique where the client passes an UnicastRemoteObject of itself to a RMI server method that registers the clientinterface object in a Vector. Now when I do...
14
2340
by: Roger Binns | last post by:
It appears that xmlrpclib and/or SimpleXMLRPCServer always use new connections for each request. I have been trying to make them reuse the existing connection but can't find where. Is there any particular reason they go to such great lengths to call shutdown/close/finish as well as making new connection objects in xmlrpclib? I am actually using M2Crypto so the connection ultimately ends up inside SSL. Having a new connection...
0
1146
by: Oleg Paraschenko | last post by:
Hello, I'd like to introduce an article which might be of some interest: Reusing XML Processing Code in non-XML Applications HTML: http://uucode.com/texts/genxml/genxml.html PDF: http://uucode.com/texts/genxml/genxml.pdf <abstract>
0
282
by: Mike | last post by:
Greetings, I have a COM+ component whose integrity I would like to maintain. In other words, I do not want to rebuild it in the .net environment. How do I go about reusing the DLL in a .net project? Thanks!
9
2354
by: Alan | last post by:
Using VC++ (1998) compiler with PFE32 editor in Win2K Pro SP4. (DigitalMars CD on order. ) The program (below) instantiates a class and then deletes it. I would have thought that reusing the deleted pointer would have caused an exception, but it does not - or my coding or thinking is incorrect? #include <iostream> using namespace std;
7
1986
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename(); somename.show() Note that the class name "somename" is reused for the variable name.
8
1444
by: John M. Gabriele | last post by:
I'm putting together a small site using Python and cgi. (I'm pretty new to this, but I've worked a little with JSP/servlets/Java before.) Almost all pages on the site will share some common (and static) html, however, they'll also have dynamic aspects. I'm guessing that the common way to build sites like this is to have every page (which contains active content) be generated by a cgi script, but also have some text files
4
9060
by: Old Wolf | last post by:
#include <stdio.h> #include <stdarg.h> Is this safe: void foo(const char *fmt, ...) { va_list ap; va_start(ap,fmt);
2
1191
by: Andy Fish | last post by:
Hi, With languages like c# having namespaces, I was wondering what's the recommended practice for reusing source code. I'm not talking big stuff like log4net that's version controlled and usually distributed as binaries, just packages of a few classes. a typical small component will be distributed as open source with a copyright statement and will usually include a project file and use a namespace of the author's own choosing....
0
9387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.