473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Variables accessible as window props in all browser DOMs?

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
4 3592
"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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Smithz | last post by:
Hi there, I have implemented a login sequence using session variables for a PHP/MySQL DB which I took over development. I read some books and this NG and it seemed straight forward. However the...
9
by: Pack Fan | last post by:
I've noticed that session variables will persist on Mac IE even after all browser windows have been closed. One must quit the program to clear the session variables. This presents a security risk...
5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
8
by: alanstew | last post by:
With the body tag calling out 'window onload', a function with a 'window.open' fails at the 'window.open' line. If I cut out the body tag, the function executes as normal. At first I thought it...
16
by: Java script Dude | last post by:
To all Mozilla JS Guru's (IE dudes welcome), I have spent the last three years developing complex DHTML applications that must work in IE 5.5sp2+ but I use Mozilla 1.3+** to do all my...
31
by: Benno Bös | last post by:
If I use the following construct in the frame "main" for a link to an extern site: <A HREF="http://www.any.xy" TARGET="extern"> the Browser is creating the window "extern", loading the page...
18
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that...
26
by: BillE | last post by:
Some ASP.NET applications use Session Variables extensively to maintain state. These should be re-written to use viewstate, hidden fields, querystring, etc. instead. This is because if a user...
2
by: Weedget | last post by:
Hello! I've small problem with my JS code. On my page i've button which executes code from below: var newWindow; var props =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.