As a student and someone fairly new to web development, I'm experiencing the following seemingly strange problem in Javascript - but only in IE (IE9 to be precise; haven't yet tried IE10). In Firefox, the script works without any problems.

The code is like this:

item = document.createElement('li'); alert("alert1");
message = document.createTextNode("Hallo"); alert("alert2");
item.appendChild(message); alert("alert3");    

In IE, alert1 and alert2 are shown, but not alert3. In other words, IE9 chokes on the third statement above (the appendChild), and never reaches alert3. Strange indeed.

Using Google, I've found that there are known problems with appendChild in IE (at least in earlier versions). But none of the problems described, or the workarounds suggested, seem to match my simple code, as cited above.

Thanks in advance for any suggestions.

Edit to add: The console gives the following error message: "object doesn't support the property or method appendChild".

有帮助吗?

解决方案

item is a native method in IE's window object.

You can rename your variable, or declare it properly:

var item = document.createElement('li');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top