View Full Version : Adding the same script twice to a page?

07-27-2002, 10:20 PM

I know this is a common question but I still haven't found an answer that applies to my problem. I have written a simple countUp script and want the users to be able to add more then one counter to the page.

If I run it with two counters I get the two counters on the page at the desired place and they are counting but in IE 5.5 and IE 6 (the two versions I tried on) it stops counting after a while and the whole browser hangs. Under Netscape 6 it doesn't behave like that.

I have read the article at http://www.javascriptkit.com/javatutors/multiplejava3.shtml and it didn't help me much because I do not have the script twice on the page and I want the print out to be on a specific place. Maybe I have to have the script twice and rename the main function in one of them?

If I put a script in the head-section and then start it via body onLoad, how do I get my printout to be in the right place.

I would be very glad if someone could point me in the right direction.

The code then, well I was not sure how much to paste in to this forum so I think it is better to give you the URL to it. Note I have put only one counter on the test page so I do not hang your browsers.

Here is the code: http://www.ludd.luth.se/~bea/age_counter_test.html

Thank you for reading this far I and hope that someone out there have a solution for me :-)


Mrs G
07-28-2002, 03:06 PM
When using more than one script on the same page a common fault is that both the script are working with the same variable(s).

The first thing to do is make sure a variable of the same name is not being used in both scripts.

If it is then rename one.


var=name for the first script

var=name2 for the second script

Also check that the scripts do not use the same function name

07-28-2002, 05:21 PM
Right on, Mrs. G.

Also remember that any variables defined w/in a fucntion are visible only to that function (e.g. the variable is said to have "function scope"). Variables with different function scope (e.g. defined in different functions) can have the same name.

If you want a variable to have visibility across functions, define it outside of the functions (e.g. "global scope"). When you do this, NOW you must be careful about duplicate variable names. Remember that Javascript is case sensitive so Apple and apple are different. - BTW it's a BAD idea to make variable names different only by upper/lower case but otherwise the same name.

As a general coding principle you want to use global scope variables as little as possible. A universal counter might be a reasonable exception.


your link to the code displays the entire thing as one contiguous line (soft wrapped in the window) so I cant read it.

07-28-2002, 06:54 PM
...to be different for the two scripts then I do not run the same script twice on the page. It seams like what I want to do is impossible in javascript then :-(

I wrote the code in Emacs and I guess that is why you do not get the line wraps . What did you open the code in? If I open the code is e.g. Notepad it shows up on one line but if I open it in WordPad it is correctly wrapped.

07-29-2002, 06:38 AM
can we see the file this is all in? i think i know what the problem is, but it would be hard for any of us to be sure, without seeing the web page you're making

07-29-2002, 10:16 AM
It is possible to have the same script run twice on a page, provided you employ object-oriented programming. The script you include would simply provide the class constructor and functions, and then you could build new objects at runtime.

Here is an example of a javascript constructor function:

function objcounter(start, interval) {
this.time = start;
this.interval = interval;
return this;

After the constructor is declared, you build it like you would any other object in JavaScript, like so:

var mycount = new objcounter(0, 100);

Any functions using the counter would then refer to mycount.time and so on.

07-29-2002, 12:49 PM

I will try the suggested solution tonight when I come home. Did some coding here at work to test it at it seams to solve my problem :-)

07-29-2002, 01:27 PM
The URL was in my firt posting: http://www.ludd.luth.se/~bea/age_counter_test.html

07-29-2002, 09:50 PM
i meant i wanted to see the page that you're working on. i think i have an easy solution to the problem, but it depends on whether i'm understanding it right. it's lots easier for me to come up with an answer when i can see the code. otherwise, i'm just guessing.

07-29-2002, 10:02 PM
I'm writing the script for some friends who wanted to have multiple counters on their childrens web pages (they are not familiar with javascript at all and are doint the homepages in e.g. FrontPage). I'm not in the need of multiple counters my self. I'm using the counter on that page and you could see it as I wanted to have two different counters in the Example part on that page.

07-29-2002, 11:00 PM
sorry, my mistake.

i went back and looked through your code, and came up with the following: i was wrong about my hopefully simple answer, and, probably this has to do with the way Explorer works, and there's probably not a whole lot you could do about it, without rewriting the function. i'm basing this on the following:

1) this problem doesn't show up in mozilla, or, not that i noticed.
2) the code works for a while, and then eventually hangs.

mozilla is much pickier (reads "better") than IE. it would be one thing if IE accepted the code, and mozilla hung. that would suggest a problem in the code. but, not only does mozilla seem to like the code, IE also likes the code for a while, and then hangs. that sounds almost like a problem with memory. remember, javascript variables aren't typed, and can chew through ram.

exactly how you ought to rewrite the function to take care of this, i couldn't say. using forms and textboxes might be a little more browser friendly, but i don't know if that would help with memory problem, supposing that that is indeed the problem.

07-29-2002, 11:23 PM
Why did the picky mozilla accept my code and not IE? I'm also guessing that is is a memory issue and then I'm totally out of options how to solve it :-(

I will try the solution suggested by Algorithm but if it is a memory issue then I do not know how that would help.

Thank you for your time and for trying to help me.


Ps. There was forms from the beginning but I thought the code should be easier to understand for the users without.

07-30-2002, 02:19 PM
Yo! Did anyone read Algorithm's answer?