By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

Variables accessible as window props in all browser DOMs?

P: n/a
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))!='unde fined');
}

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 =)

--
There are 10 kinds of people: Those who know binary and those who don't.
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Daniel" <so************@i-get-virus-and-spam.com> writes:
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))!='unde fined');
}
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:7k**********@hotpop.com...
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

--
There are 10 kinds of people: Those who know binary and those who don't.
Jul 20 '05 #3

P: n/a
"Daniel" <so************@i-get-virus-and-spam.com> writes:
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.varthatmight exists)

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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:1x**********@hotpop.com...
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.


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.
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.


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


--
There are 10 kinds of people: Those who know binary and those who don't.

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.