Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

whitenoiseie

macrumors newbie
Original poster
Jun 27, 2004
2
0
Hey Gents,
Hopefully you can help out. To quickly explain. I've got a set of arrays in JavaScript that are declared (via PHP) on my page. However as there dynamic some will not be created. Essentially the test below should check if there is such an object, if it exists it skips the test, if not, theres an alert printed out. Now this works fine in Safari on Mac OS X, I have yet to try it on any PC but I'm assuming it probably works on NN and IE there. So the only problem is IE 5.2 on the Mac... It seems that if the variable(newOptions) is undeclared it doesn't event come up undefined in a typeof test. Does anyone know a workaround?
Take a look below please:

<script language="javascript">
function fillDropDown(){
if (typeof(eval(newOptions))=='undefined'){
} else {
alert('this is happening');
//Other Code to execute
}
}
</script>


Anyone got any ideas how to write a workaround for IE on the Mac? Or indeed is this something that doesn't work on a PC either?
Thanks guys! :)
whitenoiseie
 
As it happens I just tried it on Win XP IE 6 and it doesn't work there either!
I'm shocked! Does anyone know how to do this on as many browsers as possible... I mean surely its possible to detect undeclared variables quite easily?
Thanks again guys.
Whitenoiseie
 
IE does not like performing operations on undefined types. Just try
if (typeof newOptions == "undefined" ) {
 
Try leaving out eval()

With no variable called newOptions defined this:

alert(typeof(newOptions) == "undefined");

works out to "true" for me in IE 5.2. The syntax ajs posted also works correctly for me.

I think the problem is in the eval() - you're getting an error because of the undefined value before you can test for it. For example, running this code in Mozilla:

var test = eval(newOptions);
alert(typeof(test) == "undefined");

results in an error on the first line saying that "newOptions" is undefined. At that point the script has already stopped running and you never get to the typeof() test. I would try your syntax again just leaving out the eval().
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.