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 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Convert String to Var

    Hi,

    I have this problem. I parsed the URL to grab a particular portion. That portion is a variable. That variable holds text that I want to print on screen. But the parsed variable is a string and when i write document.write(parsed) it only prints the string value not the variable value. How do I convert the parsed string into a variable name? Here's the code:

    function getArticleName() {
    var getName = window.location;
    var position = getName.toString().indexOf("html");

    // find the index value of html and then add 5 to get to beginning of article name
    position = position + 5;

    var articleTitle = getName.toString().substring(position, getName.toString().length);

    document.write(articleTitle);
    }

    The document.write at the end only prints the parsed URL portion, not the value of the variable I want. How do I resolve this?


    Justin

  • #2
    Registered User
    Join Date
    Jul 2002
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nevermind i figured it out. i just used eval(parsedstring) and it worked.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    eval is unnecessary. Search eval in this forum to see the evils of eval.
    The better solution is to use the window object.

    window[parsedstring]

    All global variables and functions are essentially member variables and functions of the window object. They can be access through the commonly used dot notation (e.g. window.str or window.myfunc()) and bracket notation (e.g. window['str'] or window['myfunc']()).
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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