views:

1053

answers:

4

ok, people here is my amazing antigravity library. I'm trying to define it as an object literal, which, uh, i read all the cool kids are doing in order to to encapsulate their libraries. Basic tests indicate it's working properly, but I cannot seem to see document.body from inside my object.

what should happen is when I click on the page "It is working" should alert. Instead I get a document.body=null error. what gives? Where is my body? why can I not set a mouse event for it? I have wasted my whole friday day with this! Gah!

antigrav={
activate:function(){
 // turn on antigrav
 document.body.onmousedown = antigrav.startPan();
},

startPan:function(event){
 alert('it is working!');
},

}

document.onload=antigrav.activate();
+2  A: 

You probably want to do

document.onload=antigrav.activate;

With parenthesis, you are setting document.onload to the result of running antigrav.activate(), instead of indicating you want antigrav.activate to run as the document.onload handler. Therefore, activate() was getting run in order to make the assignment, before the document was loaded, hence document.body wasn't defined yet.

You would want to do the same thing for the onmousedown handler assignment:

document.body.onmousedown = antigrav.startPan;

Trying this out, I also had to switch to window.onload...

Also, watch out for trailing commas in object literal definitions. Firefox is ok with them, but IE chokes. So, altogether:

var antigrav={
    activate:function(){
        // turn on antigrav
        document.body.onmousedown = antigrav.startPan;
    },
    startPan:function(event){
        alert('it is working!');
    }
};
window.onload=antigrav.activate;
Jordan Liggitt
This all seems reasonable and promising, but now the script does nothing at all. :(
updated with more fixes
Jordan Liggitt
These are good fixes, but I would do away with the document.onload entirely and just add a script tag at the bottom of your document that calls "antigrav.activate();". No need to wait for onload, as long as the dom is complete you can safely fire your script.
Prestaul
A: 

Looks like you got stuck on the same problem: http://stackoverflow.com/questions/178696/why-am-i-getting-this-javascript-runtime-error

Ates Goral
A: 

ok here is the current status:

var antigrav={
activate:function(){
 // turn on antigrav
 alert('started');
 document.body.onmouseup = antigrav.startPan;
 // make flash shield
},

startPan:function(event){
 alert('dude, you are panning!');
},


}
antigrav.activate();
//document.onload=window.antigrav.startPan;

it gets as far as "alert('started'), but still throws "error:document.body is null" the commented out lines have the same result...

using window.onload seemed to work... see the latest edit above
Jordan Liggitt
A: 

Ok, people, I am a fool, but you were all very helpful anyway.

The /real/ problem, and nothing to do with anything, and reveals a bizarre quirk of body.onclick behavior:

in my /HTML/ I had tried to set up a big document to test my clicks on:

<html>
<script src="antigrav.js"></script>
<body onload='test()'>
<!--<body onload="antigrav.activate();"  >-->

    yo, fool, this is the body!

    <div style="position:absolute; top:2000px;  left:2000px;">
        it is mad wide and tall!
    </div>

</body>
</html>

To stretch the body, I placed a div over at 2000px, 2000px. the scrollbars are there, the body must be huge, right? In reality, all that space between the div has /nothing at all/ in it - including a body element, I guess? You have to click on the actual text to trigger document.body.onclick...

Oddly, (and this is the revealed "bizarre quirk") if you give the body tag an onclick="foo()", then foo() is triggered when you click anywhere on the page, including the nonexistent body area. I have no idea why this is, but that was ctually the problem. thanks for your help people, you pointed out some other good techniques too.