Forum Moderators: open

Message Too Old, No Replies

Javascript variables in javascript?

         

jasoo

10:33 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



Hello all,

I was wondering if it is possible to get a list of all variables within the Javascript itself. Heh, kind of hard to explain, but it's quite simple what i want to do. Here's some pseudo code:

<html>
<script>
var blah = 5;
var hah = "test";
</script>
...
<script>
for (var x in <THIS PAGE>)
{
alert(x + " = " + this[x]);
}
</script>
</html>

RUN OUTPUT:
...
blah = 5
hah = test
...

I've already tried using "document" and "this" for <THIS PAGE> but they don't work. I mean it will alert certain variables that are inherent to those, but it will not alert "blah" or "hah".

Any help is appreciated.

Jason

Bernard Marx

10:53 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice try! Unfortunately it won't happen - although in some sense it should.

Global variables are properties of the window (in a browser anyway). However, like some other properties they don't show up in a for..in loop.

There is an exception, though. If you really could be bothered, you could set your variables, not like this:

var a = 5

but instead like this:

window.a = 5

or, for functions..

window.functionName = function(){ ...blah...}

[Now the call can't come before the function]

Run this code:

<SCRIPT>

// set A normally
var A = "hello, I'm A"
// check for A as explicit prop of window
alert(window.A) // or window["A"]

// set B as explicit prop of window
window.B = "hello, I'm B"

// check for var, B
alert(B)

// loop through window properties
// A doesn't show up
// but, B does (probably at the bottom)
var obj = window
var str = ""
for(var p in obj)
str += p + ": " + obj[p] + "\n"
alert(str)

</SCRIPT>

You were using this. That'll work too (self as well, I reckon)

jasoo

11:38 pm on Jun 18, 2004 (gmt 0)

10+ Year Member



Wow, thanks for the super fast reply and suggestions!

I was kind of expecting a "no". I'm really just doing this for my own amusement anyway (I think JS is a pretty fun language to mess around with and make it do things it probably shouldn't :) ).

I've actually come up with another way of accomplishing this! Why not just parse it myself? Meaning: just go through document.documentElement.innerHTML and find all occurences of var. Haha, it'll take some time to write up the code, but it will definitely be fun (plus it makes me look like I'm doing work when I'm really not).

If anyone's interested, I'll post the code here once I'm done.

Bernard Marx

11:50 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting, and perhaps in the meantime you could supply us with your psychiatrist's email address.

Counting "var". Sounds fun. Remember that [blank] can take a [blank] [blank]. This will make it much easier. Also, you need only [blank] through [blank].

[I wouldn't dream of ruining it for you :)]

Rambo Tribble

1:41 am on Jun 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, and remember that one var declaration can set multiple variables.

Purple Martin

12:03 am on Jun 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another thing to remember: variables don't have to be explicitly declared. Or to put it another way, there is no need to use "var" at all (although it is good programming practise to always use it).

This will be treated as a variable declaration if the variable has never been used before:

foo = "bar"