Jump to content

How to pass this to an event handler?


tinfanide
 Share

Recommended Posts

<script type="text/javascript" src="a.js"></script><script>func("f");function f(){alert("this is a function.");}</script>

// a.jsfunction func(a){document.body.onmousedown = a;}

I want to pass "f" to the handler in the file a.js.But it says

// IE debugger SCRIPT5007: Unable to set value of the property 'onmousedown': object is null or undefined
Link to comment
Share on other sites

Well, no error in your code tho, but the only thing you aint doing right, is that you are passing a string ('f') into the func function, instead of a reference. It should be:
func(f) // without quotes!

This doesn't work.I think the problem mainly lies in the a.js filewheredocument.body returns null (of course there is no document.body within the a.js file) It seems that I cannot store the mouse event in a seperate JS file.
Link to comment
Share on other sites

Yes. It works.Thanks for you all discussing this code.

<script type="text/javascript" src="a.js"></script><script> // passing a function does not need quotes ("")func(f);function f(){alert("this is a function.");}</script> // a.jsfunction func(a){   // It seems that after loading the HTML document, loading the js file precedes loading the document.body.   // It seems that that's why document.body is null when loading this js file   document.onmousedown = a;}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...