Click to See Complete Forum and Search --> : how to get defined variables ?


d_ted
01-05-2007, 05:11 PM
hi
i would like to make a script that users can declare their variables
is there a method to find what variables are defined?
something like this:

// user defined vars

var usrvar1=1;
var usrvar2=2;

// my function

function show_vars() {
foreach( existent variables) {
alert( variable_name + variable_value);
}
}

i hope i'm clear enough
thank you

Overstatement
01-05-2007, 05:20 PM
If you're letting users define their own variables, you have to parse their requests. If you process their requests, you can store their variable names in your own array. If you have their variable names, you can get their variable values with alert(eval(VariablesName));(I think this is legal....)

ricp
01-05-2007, 05:23 PM
Yes and no is your answer.

The properties of any object (except ActiveX) can be cycled through using..

for(item in object) { }

..the problem you have is that there is no real way to tell if something is a default property or if it's dynamic. JavaScript does have some primitive datatypes, so you can tell if something is a string or a function but that's about your limit.

Let's look at variables in the global scope (which is the window object), take this code for example..


<html>
<head>
<script type="text/javascript">
var foo = "bar";
function showWindowProperties() {
for (windowProperties in window) {
alert(windowProperties+" = "+window[windowProperties]+" \n\n(type: "+typeof(window[windowProperties])+")");
}
}
onload = showWindowProperties;
</script>
</head>
<body>
</body>
</html>

Now if you save off that page and run it you will see amongst the alerts the "foo" variable you declared in the page, however there are a lot of other properties too.

Hope that's some help.

ricp
01-05-2007, 05:27 PM
... you can get their variable values with alert(eval(VariablesName));(I think this is legal....)

That is legal, but again you are relying on evaluating the variable to find out if it's correct. In the same way you evaluate "function()" for setTimeout - and you know how much I love that.. :rolleyes:

Basically if you are looking for variables/properties/objects in the local scope, like the eval you used above would, replace that with..

alert(window[VariablesName]);

..thus a straight lookup and no runtime compiling with eval.


ps: if you are wondering, I have only once needed to use eval where nothing else will work in it's place and that was down to the way IE parses .innerHTML content.

Overstatement
01-05-2007, 05:30 PM
I knew there was another solution that had something to do with the [] operator. I still don't understand it. It seems like anything that can be refered with [] can also be refered with the dot (ie window.variablename), is that right?

ricp
01-05-2007, 05:35 PM
More or less, JS is not the most refined of languages and it let's you get away with lots of things other (more strictly typed) languages won't.

object.propertyName and window["propertyName"] are more or less the same thing. The nice thing about the for (item in object) loop is that the item will contain the actual string name reference to the property.

d_ted
01-05-2007, 05:41 PM
thanks for fast reply
i need to findout the names of the variables set by the user
so i guess the code provided by ricp will help me
i will test the js and let you know

ricp
01-05-2007, 05:43 PM
All I can suggest d_ted, is that when you allow people to create their own variable, you append a prefix or suffix to the name they provide. That's about the only (sensible) way I can see you being able to distinguish between default and dynamic properties.

d_ted
01-05-2007, 05:54 PM
yap
this is what i was thinking too
ok, before this i made a test with:
ie7,ie6,ie5.5,opera9, ff1.5
only ff showed me the foo var
the oter browsers did not
any solution?

d_ted
01-05-2007, 06:16 PM
me again
not working in konqueror too (linux) si i guess it won't work on safari,being the same rendering engine

uhh

mrhoo
01-05-2007, 08:02 PM
d_ted, why not create a single global Uservars and let user input populate it with name-value pairs? if Uservars is global, so will be its members.
What are you trying to do?

d_ted
01-05-2007, 08:06 PM
hi mrhoo
u mean like an multidimmension array?
or how
i must admit i'm not very good documented with this
the idea is to keep the declaration of vars very simple
so a noob ( like me :D ) can use it
maybe an example of declaration for this Uservars will make me understand
thnx

d_ted
01-05-2007, 08:14 PM
oh, sorry
what i am trying to do?
i want to make an universal unobtrusive form validator, able to validate any form in one page
there is a script like this but as far as i saw is based on prototype, and have 75k,this is too big
more is not very configurable, or i didn't study it enough

it happens that i must code after another coder, and don't want to redo all the forms he made....

and this idea came to me......

so i need to know what input the user want to validate and how
i hope is not too crazy :)

mrhoo
01-05-2007, 08:58 PM
If the forms suit your needs, then use them, but write your own code for
the application. You'll be glad you did.

Your project sounds difficult enough,
without trying to build it on top of a shaky foundation.

d_ted
01-06-2007, 06:56 AM
well, still no way to find the vars? in a crossbrowser way ?