Variables accessible as window props in all browser DOMs?

D

Daniel

Hullo =)

Inspired by another guy's questions here I've created an isset function that
works (almost) like the one in native PHP:

function isset(variablename) {
return(typeof(eval("window."+variablename))!='undefined');
}

I use it like if(isset('myVar') { alert(myVar); }

My question is if variables are always accessible like this, as properties
of the window object, in all browsers. I would hate it if it turned out not
to work in Opera or an older Mozilla or something like that.

Thanks,
Daniel =)
 
L

Lasse Reichstein Nielsen

Daniel said:
Inspired by another guy's questions here I've created an isset function that
works (almost) like the one in native PHP:

function isset(variablename) {
return(typeof(eval("window."+variablename))!='undefined');
}

As usual, you don't need to use eval. Try:
return (window[variablename] !== undefined);

The typeof of the value "undefined" is the string "undefined". It is also
the only value whose typeof is that string, so you might as well compare
directly with the value "undefined". As always when comparing to a simple
value, use the === and !== comparisons to avoid type conversion (otherwise
null == undefined).
I use it like if(isset('myVar') { alert(myVar); }
My question is if variables are always accessible like this, as properties
of the window object, in all browsers. I would hate it if it turned out not
to work in Opera or an older Mozilla or something like that.

Which variables?

The local variables of a function are properties of a variable object
assigned to that specific function invocation. They are not properties
of the global object.

The global variabels (i.e., those crated by assigning to an unbound
variable name, or by a variable declaration like "var x;" in the
global context) are properties of the global object. In browsers, the
global object has a property called "window" that refers to itself, so
you can access the global object as an object.

The check
window["name"]!==undefined
should be true in any browser, if the global object has no property
called "name", or if it has one, and its value is "undefined".

/L
 
D

Daniel

Lasse Reichstein Nielsen said:
As usual, you don't need to use eval. Try:
return (window[variablename] !== undefined);

The reason I'm using eval is to be able to check for variables or objects
belonging to other objects, e.g.

isset('actionobject.loveobject.varthatmightexist')
Which variables?

Any user-created variable or object in the current document that you can
address using dot notation.
The local variables of a function are properties of a variable object
assigned to that specific function invocation. They are not properties
of the global object.

Yep =)
The check
window["name"]!==undefined
should be true in any browser, if the global object has no property
called "name", or if it has one, and its value is "undefined".

And is the dot notation for the window object available also in any browser?

Thanks,

Daniel
 
L

Lasse Reichstein Nielsen

Daniel said:
The reason I'm using eval is to be able to check for variables or objects
belonging to other objects, e.g.

isset('actionobject.loveobject.varthatmightexist')

You actually want to check two different things in one function.
1) Whether a global variable is defined
2) Whether an object property is defined

I would recommend splitting the two, a they are conceptually very
different.

The first is the hardest, since just writing
x!==undefined
gives an error if x is not defined (not a property of the global object).
That is why you access it throught a reference to the global object
(either "window" or "self").

The second is easier, since object property access always work, it just
gives "undefined" if it isn't defined.

I'll give you two functions:

function isSetGlobal(name) { // name is a string
return (window[name]!==undefined);
}

function isSetProperty(prop) { // prop is a value
return (prop !== undefined);
}

It is wastefull to write "actionobject.loveobject.varthatmightexists"
as a string when you can just write it directly:

isSetProperty(actionobject.loveobject.varthatmightexists)

It even has the advantage of using the same scope as where it is
used, so it can test properties of objects referenced by local variables
as well.

Shouldn't you test the objets on the path?
Any user-created variable or object in the current document that you can
address using dot notation.

Actually ... why? I generally find it much easier to test directly with
... !== undefined
than to call a function to de the same job.

Will you ever use the function with anything but a literal argument?
That is, will you write something like
isset("myVar"+i)
?
And is the dot notation for the window object available also in any browser?

I'm not sure what you mean.

The global object is an object. The global variable "window" references it.
You can access properties of any object using either the dot-notation
or the square-bracket-notation. So, "window.foo" should work in any
browser with Javascript.

/L
 
D

Daniel

Lasse Reichstein Nielsen said:
You actually want to check two different things in one function.
1) Whether a global variable is defined
2) Whether an object property is defined

I would recommend splitting the two, a they are conceptually very
different.

Yeah, I'm working on that... I would like one function call to do both, but
it's getting hard ;)
The first is the hardest, since just writing
x!==undefined
gives an error if x is not defined (not a property of the global object).
That is why you access it throught a reference to the global object
(either "window" or "self").

The second is easier, since object property access always work, it just
gives "undefined" if it isn't defined.

That's why I wanted the dot notation starting with "window.", so as not to
get errors. Not sure it's gonna end up that way, though...
I'll give you two functions:
< ... >

You should take my function example with a grain of salt, as it was simply
put there to support the specific question I had. The actual functions are
much more advanced =) But thanks.
browser?

I'm not sure what you mean.

The global object is an object. The global variable "window" references it.
You can access properties of any object using either the dot-notation
or the square-bracket-notation. So, "window.foo" should work in any
browser with Javascript.

That's exactly what I wanted to know =). I seem to remember (may be wrong)
that in some situations, one browser would accept dot notation while another
would require brackets to address certain elements. I wanted to make sure
that any browser (with Javascript) would understand "window.foo" as a
reference to foo declared in the current document.

Thanks :)

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top