Howto: avoiding multiple copies of a script on the same page

A

andre.roberge

I'm using a js script (approx. 300 lines) from someone else to create
dynamically some web pages, using Python. Unfortunately, I am not
familiar with javascript :-(

I now want to reproduce the same functionality given by the script at
multiple points in the page. Basically, the script goes like this

/* whole bunch of functions with things in them like
input.value
input.focus()
input.selectionEnd
*/

At the very end, I have
function init() {
input = document.getElementById("input");
}

with the appropriate html code (with id="input") appearing on the web
page.

One obvious way to do it is to generate, from Python, multiple copies
of the entire script with [input] replaced everywhere by [input1,
input2, input3, ...], but this doesn't strike me as a very smart thing
to do.

I have read the faq, and didn't see anything helpful (with the
exception perhaps of a long list of potential sites to wade through,
not really aware of what I could be looking for.) Any ideas, pointers
to specific web sites where such topics are covered, etc., would be
much appreciated.

André
 
R

Randy Webb

(e-mail address removed) said the following on 5/12/2006 1:56 PM:
I'm using a js script (approx. 300 lines) from someone else to create
dynamically some web pages, using Python. Unfortunately, I am not
familiar with javascript :-(

I now want to reproduce the same functionality given by the script at
multiple points in the page. Basically, the script goes like this

Post a URL to a sample page. Without seeing how the script is written it
is impossible to answer you. If it is written to accept input as a
parameter, then it may be slight modifications or a simple call to do
what you wanted. If the script is hard-coded for one field, then it
would have to be re-coded to accept parameters. Or, a combination of both.
 
A

Andre

Randy said:
(e-mail address removed) said the following on 5/12/2006 1:56 PM:

Post a URL to a sample page. Without seeing how the script is written it
is impossible to answer you. If it is written to accept input as a
parameter, then it may be slight modifications or a simple call to do
what you wanted. If the script is hard-coded for one field, then it
would have to be re-coded to accept parameters. Or, a combination of both.
--

This is where I took the script from:

http://projects.amor.org/misc/browser/httprepl.html

André
 
A

Andre

Randy said:
(e-mail address removed) said the following on 5/12/2006 1:56 PM:

Post a URL to a sample page. Without seeing how the script is written it
is impossible to answer you. If it is written to accept input as a
parameter, then it may be slight modifications or a simple call to do
what you wanted. If the script is hard-coded for one field, then it
would have to be re-coded to accept parameters. Or, a combination of both.

I posted a link in my first reply to this message. However, it appears
to my newbie's eyes that the Javascript Best Practices site contains
the info that I need... In any event, thank you for that link!
André
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 12 May 2006 10:56:17 remote, seen in
I'm using a js script (approx. 300 lines) from someone else to create
dynamically some web pages, using Python. Unfortunately, I am not
familiar with javascript :-(

I now want to reproduce the same functionality given by the script at
multiple points in the page. Basically, the script goes like this

/* whole bunch of functions with things in them like
input.value
input.focus()
input.selectionEnd
*/

At the very end, I have
function init() {
input = document.getElementById("input");
}

with the appropriate html code (with id="input") appearing on the web
page.

One obvious way to do it is to generate, from Python, multiple copies
of the entire script with [input] replaced everywhere by [input1,
input2, input3, ...], but this doesn't strike me as a very smart thing
to do.

I have read the faq, and didn't see anything helpful (with the
exception perhaps of a long list of potential sites to wade through,
not really aware of what I could be looking for.) Any ideas, pointers
to specific web sites where such topics are covered, etc., would be
much appreciated.


Where code is substantially repetitive, use a function call for each
instance with parameters to customise it.

Those three lines, albeit unreasonable as posted, could be written as

function Tintin(champs) {
champs.value
champs.focus()
champs.selectionEnd }

TinTin(input)

which is of course longer; but now each repeat adds one line instead of
three.

The reason that it is not in the FAQ is probably that using functions /
methods / subroutines for repeated code is a fundamental part of
programming, independent of language. But sections 4.5 4.10 4.15 4.16
4.22 illustrate such use, or at least the provision of such functions.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top