SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast jumboshrimp's Avatar
    Join Date
    Aug 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting values of eval()'d variable names..

    i'm setting string variables using eval. But when i check their type i get back "object". The string inside eval evaluates to: var the_variable = "value";

    so i can't understand why the type would say object. does anyone know if this is common behavior?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This gives me "string":
    Code:
    <html>
    <head>
    <script type="text/javascript">
    window.onload = function () {
    	var someText = "var myVar = 'testing 1.2.3';";
    	eval(someText);
    	alert(typeof myVar);
    }	
    </script>
    </head>
    <body>
    </body>
    </html>

  3. #3
    SitePoint Enthusiast jumboshrimp's Avatar
    Join Date
    Aug 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i just got the same result.. I realized what i was doing. I wasn't doing the typeof inside the function i was doing the eval in. For some reason i was assuming that i was setting the eval'd variables globally. So, What would be the best way to get these variables global? declaring a "mother" array or object in the global scope? or is there a way to make these variables available outside the function?

    Thanks for the help!

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do it without using eval at all - create properties of the window object and then you can reference them no matter which function you're in:
    Code:
    window.onload = function () {
    	window["myVar"] = "testing 1.2.3";
    	window["some other variable"] = "other ting";
    	
    	alert(typeof window["some other variable"]);
    	alert(typeof window["myVar"]);
    }

  5. #5
    SitePoint Enthusiast jumboshrimp's Avatar
    Join Date
    Aug 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm using eval because i don't know what my variable names are going to be. My statement looks like this:

    Code JAVASCRIPT:
    eval('var '+to_be_determined+' = "'+some_value+'";');

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do the same thing without eval
    Code:
    // example
    var to_be_determined = "this is some variable name";
    
    window[to_be_determined] = some_value;

  7. #7
    SitePoint Enthusiast jumboshrimp's Avatar
    Join Date
    Aug 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's what i ended up doing. thanks for the help jim.


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
  •