SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Oct 15, 2007, 11:12 #1
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?
-
Oct 15, 2007, 11:23 #2
- 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>
-
Oct 15, 2007, 11:27 #3
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!
-
Oct 15, 2007, 11:50 #4
- 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"]); }
-
Oct 15, 2007, 12:02 #5
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+'";');
-
Oct 15, 2007, 12:05 #6
- 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;
-
Oct 15, 2007, 12:46 #7
that's what i ended up doing. thanks for the help jim.
Bookmarks