【发布时间】:2011-06-08 19:06:47
【问题描述】:
我正在维护一些包含太多document.write 实例的代码。例如,代码将 document.write 一个 div,然后使用 DOM 找到该 div,然后将 <img> 附加到它。
我尝试重构相关部分,使其看起来更像这样:
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 );
这是在 IE 中引发错误,提示“Internet Explorer 无法打开 Internet 站点 http://blah.com。操作已中止。”
有人告诉我这是因为我确实需要使用document.write。我希望有一种替代方法可以让我使用上述方法,而不是强迫我将我的节点变成一个字符串(例如“”)并通过document.write 附加它。谁能建议我的问题的解决方案?
【问题讨论】:
-
“blah.com”或任何其他 URL 与该特定代码有什么关系? (换句话说,是什么让您认为该代码与该错误有关?)(或者“blah.com”是包含图像的域?)
-
我认为错误消息是相关的。网址 blah.com 不是。
-
设置 .src 为图像后,它会获取它。
-
非常有趣——您是说这可能导致操作中止吗?
-
好吧,如果图片 URL 是伪造的,那可能会导致错误,但它只是一张损坏的图片,看起来通常不会引起严重的投诉。
标签: javascript internet-explorer cross-browser