Why am I getting this Javascript runtime error?

2019-04-10 06:58发布

问题:

I've got the following JavaScript on my web page...

64    var description = new Array();
65    description[0] = "..."
66    description[1] = "..."
...
78    function init() {
79        document.getElementById('somedivid').innerHTML = description[0];
80    }
81
82    window.onload = init();

In Microsoft Internet Explorer it causes the following error...

A Runtime Error has occurred.
Do you wish to debug?

Line: 81
Error: Not implemented

Line 79 executes as expected.

If line 79 is commented out, it still throws the error.

If I comment out line 82, then the function does not execute and there is no error.

回答1:

Shouldn't line 82 read:

window.onload = init;

When you do "init()" it's a call to a function that returns void. You end up calling that function before the page loads.



回答2:

To preserve any previously set onload functions try this

var prevload = window.onload;
window.onload = function(){
    prevload();
    init();
}


回答3:

Try to add an envent listener for 'load' instead, or use the declarative syntax <body onload="init()">.

EDIT: Additionally, saying window.onload = init(); sets window.onload to the result of calling init(). What you mean is window.onload = init; (a lambda expression). This is bad practice still, as it overwrites other things that might be bound to window.onload.



回答4:

In addition to the onload fixes proposed here, also check to see if there are multiple elements with that ID, I believe IE will return a collection of all elements with that ID, in which case you would need to select the intended item out of the collection before accessing that property or ensure you are using unique IDs.



回答5:

Try running it in FireFox with the FireBug plugin enabled. This will allow you to debug the javascript