Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Adding the same script twice to a page?

    Hi!

    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/javatut...plejava3.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 :-)

    /Bea

  • #2
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    EG:

    var=name for the first script

    var=name2 for the second script


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

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If I change the variable names...

    ...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.

  • #5
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you

    Hi!

    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 :-)

  • #8
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Answer to joh6nn

    The URL was in my firt posting: http://www.ludd.luth.se/~bea/age_counter_test.html

  • #9
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #10
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    That _is_ the page

    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.

  • #11
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #12
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    That was my problem as well

    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.

    /Bea

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

  • #13
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Answer Hidden in Plain Sight?

    Yo! Did anyone read Algorithm's answer?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •