tinfanide Posted July 10, 2012 Share Posted July 10, 2012 <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 More sharing options...
eTianbun Posted July 10, 2012 Share Posted July 10, 2012 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! Link to comment Share on other sites More sharing options...
tinfanide Posted July 10, 2012 Author Share Posted July 10, 2012 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 More sharing options...
eTianbun Posted July 10, 2012 Share Posted July 10, 2012 I know, thats why i said 'no error in your code...' I knew even tho you changed that, it will not fix the prob, but i just wanted to point that out. Link to comment Share on other sites More sharing options...
tinfanide Posted July 10, 2012 Author Share Posted July 10, 2012 So it is never possible to do this way? Link to comment Share on other sites More sharing options...
eTianbun Posted July 10, 2012 Share Posted July 10, 2012 (edited) Your code should work tho! Have you tryd it in other browsers? Edited July 10, 2012 by CodeName Link to comment Share on other sites More sharing options...
justsomeguy Posted July 10, 2012 Share Posted July 10, 2012 Are you running that code before the body has loaded? Link to comment Share on other sites More sharing options...
dsonesuk Posted July 11, 2012 Share Posted July 11, 2012 try using document on its own without '.body' document.onmousedown = a; Link to comment Share on other sites More sharing options...
Don E Posted July 11, 2012 Share Posted July 11, 2012 Using dsonesuk's suggestion and passing f without quotes as how CodeName suggested should work. Link to comment Share on other sites More sharing options...
tinfanide Posted July 11, 2012 Author Share Posted July 11, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now