1

I'm maintaining some code that has far too many document.write instances. For example, the code would document.write a div, then use the DOM to find that div, then append an <img> to it.

I've tried to refactor a relevant piece to look more like this:

var node_to_append = document.createElement("div");
//node_name is a hardcoded constant
node_to_append.id = node_name;

var i = 0;
for( i = 0; i < request_urls.length; i++) {
    var image_to_append = document.createElement( "img" );
    image_to_append.width = 1;
    image_to_append.height = 1;
    image_to_append.alt = "";
    image_to_append.src = request_urls[ i ];
    node_to_append.appendChild( image_to_append );
}
//finally, append the div to the HTML doc
document.body.appendChild( node_to_append );

This is throwing an error in IE, saying "Internet Explorer can not open the Internet site http://blah.com. Operation aborted."

I'm told this is because I needed to indeed use document.write. I'm hoping there's an alternative that would allow me to use the above approach and not force me to turn my node into a string (e.g. "") and append it via document.write. Can anyone suggest a solution to my problem?

buley
  • 28,032
  • 17
  • 85
  • 106
  • 1
    What does "http://blah.com", or any other URL for that matter, have to do with that particular code? (In other words, what makes you think that that code has anything to do with that error?) (Or is "blah.com" the domain with the images?) – Pointy Jun 08 '11 at 19:12
  • I figured the error message was relevant. The URL blah.com is not. – buley Jun 08 '11 at 19:15
  • 1
    after you set .src to an image, it will fetch it. – Dvir Jun 08 '11 at 19:17
  • Very interesting -- are you saying that might be causing the operation to abort? – buley Jun 08 '11 at 19:21
  • Well, if the image URL is bogus, that *might* cause an error, but it's just a broken image and it doesn't seem like those normally cause serious complaints. – Pointy Jun 08 '11 at 19:23

1 Answers1

2

If this code had document.writes in it, then it was being run while the page was being loaded/parsed, wasn't it? You should wrap your code in a window.onload event handler. Even better, use jQuery's ready event, or another lib's dom ready wrapper.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Thanks, yes that's correct and a good point. The previous code did not have an onload wrapper and I'm testing it out now. Do you think that could be causing the issue or is that just a good idea anyways? – buley Jun 08 '11 at 19:21
  • Looks like that might be the case. Nice resource for future inbound Googlers: xx It turned out that I appended the background element “incorrectly”, according to IE. If document.body.appendChild is executed within the body tag before the body is closed, IE will simply not load the page. xx http://bit.ly/iEnaBo – buley Jun 08 '11 at 19:25
  • It is required for code that immediately touches the DOM. If you're just defining a function (which will only be called after the DOMReady event), then it doesn't matter where you put it. The DOMReady event signals that the DOM is ready to be manipulated. – Ruan Mendes Jun 08 '11 at 19:29