SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    stops working after 1 click

    Hey all,

    On my site I'm using this little function:
    Code:
    function challenge(url,name) {
    challenge = confirm("Are you sure you want to challenge " + name + "?" );
    
    if(challenge == true)
    alert("I see you" );
    }
    And it's called from something like this:

    <a href="#" onClick="challenge('theurl', 'personsname');">Challenge This Person</a>

    (The "Isee you" will be replaced by a redirection soon)
    There are many links as there are posts (ex. http://www.fragblast.com/forums/show...&threadid=2090, if you click "Challenge" for one, and then try to click another, it gives an error on a line. I can't figure it out, any thoughts?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function challenge // creates global variable challenge
    challenge = confirm(... // over-writes global variable challenge

    When you call your function, the first thing it does it over-write itself! Variables declared by assignment are always global. Use a different name for one or the other (or declare the one in the function with var, making it local). Or....

    function challenge(url,name) {
    if (confirm("Are you sure you want to challenge " + name + "?")) alert("I see you" );
    }

  3. #3
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooh i see, so they're not unique. thanks. i thought the function names and variables were allowed to be the same since they'd be different.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nabeel
    ooh i see, so they're not unique. thanks. i thought the function names and variables were allowed to be the same since they'd be different.
    Nope. This:

    function challenge() {.....

    ...creates a global variable (window property: window.challenge) and stores a function object - JS functions are objects - in it. Everything in JS is a property of some object or another, no 'free-floating' bits of data. Gotta watch those names!

  5. #5
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh ok, thanks.

  6. #6
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also just do it like:

    Code:
    function challenge(url,name) {
      var challenge=confirm("Are you sure you want to challenge " + name + "?");
      if(challenge) {alert("I see you" );}
    }
    When you declare the scope of the variable challenge as private for the function challenge nothing should happen.

  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even though that works, it's bad practice to do so - there's simply no need to use variables which have the same name as the function they are within. It is unnecessarily confusing for other coders (who may have to modify your code in the future) and in this particular case you can do without the variable at all, like so:
    Code:
    function challenge(url,name)
    {
       if(confirm("Are you sure you want to challenge " + name + "?" ))
       {
            alert("I see you" );
       }
    }
    PS - why are you passing a "url" parameter which isn't actually being used?

    edit - corrected typo
    Last edited by M@rco; May 31, 2003 at 08:40.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  8. #8
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do not agree with you, Marco. You may say it's not practical but bad practice???

    When a variable is declared as private inside a function it keeps the scope of that function regardless of having another variable outside (global or not) that is called with the same name.

    Say you have a function which uses a for loop like:

    Code:
    function myfunctionA(array){ 
       for (var i=0;i<array.length;i++){
    	 alert(array[i]);
       }
    }
    and another one that loops the other way around, like:


    Code:
    function myfunctionB(array){
       for (var i=array.length;i>0;i--){
    	 alert(array[i]);
       }
    }
    Functions have their own scope space and will never interfere with the scope of a private variable.

    Aside from this I guess the url parameter will redirect the user to a new location where he can fulfill the challenge.

  9. #9
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nacho
    Aside from this I guess the url parameter will redirect the user to a new location where he can fulfill the challenge.
    yeah that's what it will do, itll redirect the user, but i don't want it to redirect it yet, because the entire 'system' thing isn't complete yet.

    i ended up using the one without a variable, its just easier. im not sure about overhead/memory that comes with using a variable in javascript, but if there is any that would avoid that too.

  10. #10
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nacho
    I do not agree with you, Marco. You may say it's not practical but bad practice???

    When a variable is declared as private inside a function it keeps the scope of that function regardless of having another variable outside (global or not) that is called with the same name.

    Functions have their own scope space and will never interfere with the scope of a private variable.
    You misunderstand me - I made it quite clear that I was referring to the use of a variable with the same name as the function itself. I'm not saying you shouldn't use variables like "i", "n", "c" for loops in global or local scope - I'm saying that it's bad practice to write code like this:

    Code:
    function myfunctionA(array){ 
       for (var myfunctionA=0;myfunctionA<array.length;myfunctionA++){
    	 alert(array[myfunctionA]);
       }
    }
    Your original version of that code (with the variable "i") is precisely how it *should* be written!!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  11. #11
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You misunderstand me
    I did not. I was trying to make my point clear playing with the scope of a variable, that's what I was talking about.

    To make it even clearer,

    there's simply no need to use variables which have the same name as the function they are within
    Yes, there is no need.


    It is not practical, I will admit it for the second time, to name a variable with the same name that the function that holds that variable though is far from bad practice.

    IMHO, if somebody as a programmer gets confused with something similar they would do better getting a real job.

  12. #12
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nacho...do you have problems reading others' posts?

    Use a different name for one or the other (or declare the one in the function with var, making it local)...
    function challenge(url,name) {
    var challenge=confirm
    Skipping the variable altogether by compacting the code is the best overall solution, imo.

  13. #13
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nacho...do you have problems reading others' posts?
    Not that I'm aware off although I admit I did not read yours properly.

    Mea culpa ... or do I have to apologize elsewhere publicly?

    IMHO, the best solution is to declare a local variable and call it ok, for example. I think , shake the earth, that you should always use a variable to hold the value that a certain function returns instead of using the function directly and loosing up that value in the air ...

  14. #14
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I give up!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  15. #15
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that it was getting interesting ...

    Cheers everyone.


Bookmarks

Posting Permissions

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