Click to See Complete Forum and Search --> : Javascript Error


dj4uk
04-29-2003, 04:00 AM
I'm getting a runtime error when I try to run a piece of JavaScript. The line that is causing the problem is the first line of an if statement:

if(QueryString != undefined && QueryString["date"] != undefined) {

The error states that undefined is undefined. Note this only happens in some browsers!

From some research I have found that the undefined keyword only works for some browsers has anyone got any more info on this e.g. which browsers it will work for? Can anyone come up with a workaround so that I can check if variables are undefined.

Help appreciated.

DJ

DrDaMour
04-29-2003, 04:06 AM
!= NULL

dj4uk
04-29-2003, 04:09 AM
That won't work - if the variable is undefined it will cause an error if you try to compare it with null.

DrDaMour
04-29-2003, 04:17 AM
Wait so you want to know if an element even exists in teh javascript? That doesn't make a lot of sense since JS really isn't dynamic in that way. You need to clarify what yru problem is.

dj4uk
04-29-2003, 04:23 AM
Ok my script builds a global array from the contents of the http header however if the http header is empty when the array isn't created and therefore is "undefined".

Charles
04-29-2003, 04:47 AM
Try if(self.queryString && self.queryString["date"]) {}.

dj4uk
04-29-2003, 04:50 AM
The problem isn't with finding the array variable the problem is that some browsers don't seem to recognise the undefined keyword.

Charles
04-29-2003, 04:58 AM
No, they understand the undefined property (http://developer.netscape.com/docs/manuals/js/client/jsref/toplev.htm#1094680) just fine. It's just that they have improperly implemented the object structure of the language. Try

<script type="text/javascript">
<!--
foo =' foo';
if (self.foo != undefined) {document.write (foo)};
// -->
</script>

DrDaMour
04-29-2003, 04:58 AM
can't your script just build some flags to say if something was made or not? Or maybe always maek the array, but not set the value?

Charles
04-29-2003, 05:00 AM
Or you can try reading my posts.

dj4uk
04-29-2003, 05:02 AM
Charles - it works fine in some browsers just not others - http://www.devguru.com/Technologies/ecmascript/quickref/undefined.html

DrDaMour - might have to try that!

Charles
04-29-2003, 05:18 AM
If you try reading my posts, you will notice that the first suggested solution to the problem doesn't use the defined property at all. Undefined object values are evaluated as false in the boolean context. The problem is that MSIE forgets that seemingly top level variables are supposed to be properties of the window object. But if you treat them that way MSIE gets it right.

If you try reading the article that you suggested you will find that the problem isn't that MSIE doesn't understand the undefined property but that "In Internet Explorer, if you attempt to utilize an undefined variable, you will get a runtime error message." But you will find that if you remind MSIE that your variable is really a property of the window object that it does indeed recognize the undefined property. And if you would try reading my second post you would see an example of this.

Padrill
04-29-2003, 05:27 AM
Try
if (!QueryString) ...

dj4uk
04-29-2003, 05:34 AM
if(QueryString && QueryString["date"]) {

Works!

Thanks for all the help guys!

Padrill
04-29-2003, 06:03 AM
You're welcome :D