debugging javascript

Paul King sciguy-Ja3L+HSX0kI at public.gmane.org
Mon Sep 1 01:48:51 UTC 2008


On Sun, 2008-08-31 at 19:48 -0400, Ian Petersen wrote:
> I saved a copy of your page to my disk and browsed to the local copy,
> thinking I could play around with it from there.  It failed when you
> tried to send null with the XMLHttpRequest.  I think you might want to
> send the empty string, instead.
> 
> For some reason, though, none of your functions seem to get called
> when I browse to the version hosted on your server.  One thing you
> might want to try is change
> 
> <script language="JavaScript">
> 
> to the more modern
> 
> <script type="text/javascript">
> 

OK, the change is made. However, isn't javascript the default in
Firefox? I understand I should get away with just
<script>
//code here
</script>
and Javascript is understood.

> It could be that Firefox isn't even parsing the script, although that
> seems a little weird.
> 

getData() is definitely called, since it successfully executes
         XMLHttpRequestObject.open("GET", dataSource);
inside the function. This is verified by firebug. It even shows me the
contents of the file retrieved. Problem is, it is not parsed into a list
for the dropdown menu.

> Also, I think you may need to change the following code:
> 
> function MouseEvent(e) {
>    if (e) {
>       this.e=e;
>    } else {
>       this.e=window.event;
>    }
> 
>    if (e.pageX) {
>       this.x=e.pageX;
>    } else {
>       this.x=e.clientX;
>    }
> 
>    if (e.pageY) {
>       this.y=e.pageY;
>    } else {
>       this.y=e.clientY;
>    }
> 
>    if (e.target) {
>       this.target=e.target;
>    } else {
>       this.target=e.srcElement;
>    }
> }
> 
> First, MouseEvent is probably a bad name for the function because the
> DOM spec defines something by the same name and you might be running
> into name conflicts.  Second, every bare reference to the variable 'e'
> after the first if should probably be referring to 'this.e'. 
>
> If the
> first if block falls through to the else case, then the variable named
> e is probably undefined, in which case every reference thereafter is
> going to be an error.

Now that I look at it, if I make 
this.e = e;
(supposing e exists), then would it not be understood that therefore
this.e.pageX already *has* e.pageX? That is, assigning e to this.e
should assign the properties and methods of e to this.e with just one
assignment, shouldn't it?

But Holzner (the author of the code) has stated in another part of the
book (Ajax Bible), that the first "if" is whether an "e" is to be passed
at all; the second and subsequent "if" statements test to see whether
the event occurred in the client area of the browser. If it does, then
it should have "pageX" and "pageY" properties. If it doesn't then it
falls back to the window object for the assignment.

Therefore, success of the first "if" does not guarantee the success of
the subsequent "if" statements. OTOH, failure of the first "if", as you
say, will cause everything else to fall through also.

> I noticed you're generating debug output with document.write().  You
> probably don't want to do that.  If you ever manage to execute a
> document.write(), the argument will replace the contents of the
> current document, rather than appending to it, so you'll lose
> everything.  It would be better to have a <div> with an id like
> "debug" that you append to using the DOM.  (ie.
> document.getElementById("debug").appendChild(document.createTextNode("debug
> comment here"));)
> 
> Finally, you may want to start out making your code work in either
> Firefox or IE and then add cross-browser compatibility code later.
> Some of your confusion may come from your attempts to make the code
> work in standards-compliant browsers and IE simultaneously.

All good suggestions, thanks for your help.

Paul 

> 
> Ian
> --
> The Toronto Linux Users Group.      Meetings: http://gtalug.org/
> TLUG requests: Linux topics, No HTML, wrap text below 80 columns
> How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
>
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list