Connecting Tech Pros Worldwide Help | Site Map

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

andre.roberge@gmail.com
Guest
 
Posts: n/a
#1: May 12 '06
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é

Randy Webb
Guest
 
Posts: n/a
#2: May 12 '06

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


andre.roberge@gmail.com said the following on 5/12/2006 1:56 PM:[color=blue]
> 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[/color]

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.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Andre
Guest
 
Posts: n/a
#3: May 12 '06

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



Randy Webb wrote:[color=blue]
> andre.roberge@gmail.com said the following on 5/12/2006 1:56 PM:[color=green]
> > 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[/color]
>
> 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.
> --[/color]

This is where I took the script from:

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

André
[color=blue]
> Randy
> comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
> Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/[/color]

Andre
Guest
 
Posts: n/a
#4: May 12 '06

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



Randy Webb wrote:[color=blue]
> andre.roberge@gmail.com said the following on 5/12/2006 1:56 PM:[color=green]
> > 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[/color]
>
> 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.
> --
> Randy
> comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
> Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/[/color]

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é

Dr John Stockton
Guest
 
Posts: n/a
#5: May 12 '06

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


JRS: In article <1147456577.491857.305590@y43g2000cwc.googlegroups .com>
, dated Fri, 12 May 2006 10:56:17 remote, seen in
news:comp.lang.javascript, andre.roberge@gmail.com posted :[color=blue]
>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.[/color]


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.

--
© 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.
Closed Thread