home :: computers :: programming :: javascript :: ie dom help

IE DOM Help

I got Test.Harness.Browser working with IE 6 SP 2 today, but decided to spend a bit of time trying to get it working with the DOM script inclusion approach instead of the XMLHttpRequest approach. The code that causes the problem is this (pre is a pre element generated with the DOM API):

el = doc.createElement("script");
el.type = "text/javascript";
// XXX IE chokes on this line.
el.appendChild(doc.createTextNode("window.onload(null, Test)"));
pre.appendChild(el);

This works great in Firefox, but IE 6 doesn’t like the call to appendChild(). It says, Unexpected call to method or property access. So I tried to replace that line with:

el.innerHTML = "window.onload(null, Test);";

Firefox is still happy, but now IE 6 says, Unknown runtime error. If I try to just append a script tag to pre.innerHTML, I get no error, but the code doesn’t seem to execute, either. In fact, pre.innerHTML appears to be empty!

Anyone have any idea how I can dynamically write to a script element that I’ve created via the DOM?

Comments & Trackbacks

Marshall Roch wrote:

IE doesn't let scripts have children. You need to test for IE and use el.text. This is what I found somewhere a while back:
  if (null == node.canHaveChildren || node.canHaveChildren) {
    node.appendChild(document.createTextNode(text));
  } else {
    node.text = text;
  } 

Theory wrote:

Thanks Marshall, that almost gets me there! I added this code:

var text = "window.onload(null, Test)";
// IE doesn't let script elements have children.
if (null != el.canHaveChildren) el.text = text;
// But most other browsers do.
else el.appendChild(document.createTextNode(text));

Now I get two new errors. The first is:

A runtime error has occurred.
Line: 53162800
Error: "Test" is undefined

When I go into the debugger, it shows me just a few bytes of binary data. Not very useful. This error goes away if I remove the line that adds the text to the script.

Once I tell it to ignore that, I get another error, Invalid argument. That's for this line a bit later: body.appendChild(pre);. Again, that succeeds if the script text stuff is commented out. I don't think that IE likes the assignment to the text attribute of a script element. :-(

Any other ideas?

Thanks!

—Theory

Lufo wrote:

Hi, i'm trying to do this:

I have functions.js with:

  function MyLibraryFunction() {
      return "Hello World";
  }

and then I have runtime.js with:

function doIncludes() {
	script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "functions.js";
	script.defer = true;
	document.getElementsByTagName("head")[0].appendChild(script);
}

function windowOnLoad(e) {
	doIncludes();
	setCookie("pepe", "Hola Mundo");
	alert(MyLibraryFunction());
}

if(window.addEventListener)
	window.addEventListener("load", windowOnLoad, false);
else
	window.attachEvent("onload", windowOnLoad);

Surprisingly, it's working perfectly in IE6 but not in FireFox. Any ideas?

sportyboy56 wrote:

Hi guys...my script also haf the runtime error,I tested my page wif Opera,no complains,only my IE6 complains this line.. tooltip.innerHTML = tip; my <div> is inside a table as I using dreamweaver to do...

Vip wrote:

there is no answer for IE innerhtml bug,is it realy a bug is there is any solution what hell is this Even Microsoft mute on this problem what a tragedy they want make money by selling bad products

Christocracy wrote:

if you're trying to attach an onload event to your window, I use the wonderful prototype.js library and attach an event like this:

Event.observe(window, 'load', function()
{
    alert('window onload');    
});

works great in both IE and FF.

intrader wrote:

I noticed this problem way back in IE4

and its is still there in IE6. It occurs when I do a replacement of the innerHTML of some table elemen. What I did back then was rearrange the target such that I did an insertAdjacent of a div. However, now, I am trying to replace a placeholder (a tbody with id), with a couple of tds. I get the "Unknown runtime error". If I replace the tbody with a div, the first td will be placed outside of the table, while the second td is replaces the placeholder. It looks like I need to do is put an id in the tr, and then insertCell to accomplish this

lowteched wrote:

this works fine

function loadContent(file){
  var scriptTag = document.getElementById('loadScript');
  var head = document.getElementsByTagName('head').item(0)
  if(scriptTag) head.removeChild(scriptTag);
  script = document.createElement('script');
  script.src = file;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script)
}

as there were issues with the code on this page. this one i found on: http://www.dhtmlcentral.com/tutorials/tutorials.asp?page=1&id=11

does the tricks. found no problems yet.

greetings,

lowteched

Powered by KinoSearch