(function() {
var myFunc = {
init : function() {
alert("I need");
document.getElementById("myDiv").innerHTML = "help";
}
};
window.myFunc = myFunc;
})();
myFunc.init();
The alert() message works but the Uncaught TypeError is thrown when it reaches the next line. Can anyone explain to me why and how to fix it? I assume it is something to do with the scoping of the function as it will work if nested within the myDiv rather than the linked .js file.
Thanks
(function() {
var myFunc = {
init : function() {
alert("I need");
document.getElementById("myDiv").innerHTML = "help";
}
};
window.myFunc = myFunc;
})();
myFunc.init();
The alert() message works but the Uncaught TypeError is thrown when it reaches the next line. Can anyone explain to me why and how to fix it? I assume it is something to do with the scoping of the function as it will work if nested within the myDiv rather than the linked .js file.
Thanks
Share Improve this question asked Nov 16, 2010 at 16:35 neiler45neiler45 31 silver badge2 bronze badges 2- Nothing looks wrong with your code. Which browser is it failing in? I tried it in FF4, IE8, and Chrome9. They all worked. jsfiddle/PffYx – James Kovacs Commented Nov 16, 2010 at 16:40
- its in chrome. the alert works fine which is why i cant understand why it gets that far but wont set the innerHTML – neiler45 Commented Nov 16, 2010 at 16:51
2 Answers
Reset to default 5Your script is executing before the document is parsed.
You should move the <script>
tag to the bottom of the document, or call init
in the onload
event.
is document.getElementById("myDiv") returning null?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745301046a4621436.html
评论列表(0条)