By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,650 Members | 1,996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,650 IT Pros & Developers. It's quick & easy.

Reusing a script

P: n/a
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.frmFormName.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
Share this Question
Share on Google+
2 Replies


P: n/a
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.frmFormName.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.getElementById(x);
....

... onclick="checkSomething('theForm');"....
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>Reuse 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="checkSomething(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="aTextInput" size="50"><br>
<input type="button" name="aButton" value="Pass the form"
onclick="checkSomething(this.form);">
<input type="button" name="aButton" value="Pass the 'text' input"
onclick="checkSomething(this.form.aTextInput);">
<input type="button" name="aButton"
value="Pass the 'text' input value"
onclick="checkSomething(this.form.aTextInput.value );">
<input type="reset" value="Reset">
</form>
</body>
</html>
Jul 23 '05 #2

P: n/a
JRS: In article <41**********************@news.newsgroups.ws>, dated
Wed, 20 Oct 2004 06:58:15, seen in news:comp.lang.javascript, Luis
<an**********@webmail.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.demon.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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.