SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot daemon's Avatar
    Join Date
    Apr 2001
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Does JS support variable variable names?

    Would something like the following php code be possible in JavaScript? If yes how.

    $array = array ( 'firstpage', 'secondpage' );
    $$array[0] = 'index.html';
    echo ( $firstpage );

  2. #2
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not aware of anything in JavaScript that would allow you to do this. I think you would have to write some extra code to assign a value to another value.

    I could be wrong.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  3. #3
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    of course!

    var first = ' mouse ';
    var cat = first;

    alert(cat);
    alert(eval(cat));

    That'd do you - right?

    * doesn't bother testing this - as per usual *

    let me know if it doesn't work.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  4. #4
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good to see someone understands the logic better than most others.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  5. #5
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HUH ?

    * doesn't understand which way and to who that insinuation / guesture is meant to swing *

    You fancy clarifying that a bit more?

    oh - and what you're mad at ?



    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  6. #6
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was meant to be a compliment Flawless, and the fire means REDHOT. Maybe I chose the wrong the smiley, but at the time I thought it was appropreate to point out that I think your JS logic is impressive. I can however now see how that could be misunderstood.

    doesn't bother testing this - as per usual
    Then again, I suppose you don't generally take compliments either.
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  7. #7
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he he - that's ok - i get what you mean now.

    Thanks for the compliment

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  8. #8
    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)
    Originally posted by Flawless_koder
    of course!

    var first = ' mouse ';
    var cat = first;

    alert(cat);
    alert(eval(cat));

    That'd do you - right?

    * doesn't bother testing this - as per usual *

    let me know if it doesn't work.

    Flawless
    All very great, but despite being along the right lines, it doesn't actually demonstrate the real answer to daemon's original question!!!

    IMHO the answer is more like this:
    Code:
    <script language="JavaScript">
    //although this is hardcoded, the "myvar" bit is just
    //a string, and so could be derived from anywhere
    var newVarName = "testvar";
    
    //ditto
    var newVarValue = "hello world!";
    
    //turn the variable name and value into a variable with
    //an assigned value (beware of unescaped quotes when you
    //do this for real)
    eval(newVarName + "='" + newVarValue + "'");
    
    //we now have a variable called "testvar", so
    //let's output the contents!
    alert(testvar);
    </script>
    daemon, does that answer your question?!
    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!

  9. #9
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .oO( You can tell when blow up dolly gets a puncture! )




    j/king mate

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

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


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
  •