Javascript appendChild不使用IE中的window.open
我有一个带有svg标记的页面。该页面有一个名为“预览”的按钮,单击该按钮将打开一个包含图像(svg)的新窗口 下面是一段在Chrome/Firefox中工作的代码,但在IE中不工作(我使用的是IE9-IE9标准模式) 如有任何建议,将不胜感激Javascript appendChild不使用IE中的window.open,javascript,internet-explorer,window.open,appendchild,Javascript,Internet Explorer,Window.open,Appendchild,我有一个带有svg标记的页面。该页面有一个名为“预览”的按钮,单击该按钮将打开一个包含图像(svg)的新窗口 下面是一段在Chrome/Firefox中工作的代码,但在IE中不工作(我使用的是IE9-IE9标准模式) 如有任何建议,将不胜感激 谢谢。IE将阻止附加在不同窗口上下文中创建的任何元素,该窗口上下文与元素所附加到的窗口上下文不同 var childWindow = window.open('somepage.html'); //will throw the exception in
谢谢。IE将阻止附加在不同窗口上下文中创建的任何元素,该窗口上下文与元素所附加到的窗口上下文不同
var childWindow = window.open('somepage.html');
//will throw the exception in IE
childWindow.document.body.appendChild(document.createElement('div'));
//will not throw exception in IE
childWindow.document.body.appendChild(childWindow.document.createElement('div'));
在处理了相同的问题之后,这是我的IE解决方案的摘录,避免了SCRIPT5022错误。谢谢你的帮助
var myWindow=window.open('about:blank','loading…','');
var myWindowDoc=myWindow.document.implementation.createDocument('http://www.w3.org/1999/xhtml','html',空);
var myWindowBody=myWindow.document.CreateElements('http://www.w3.org/1999/xhtml","主体",;
myWindow.document.open().write(“”);
myWindow.document.close();
试一试{
myWindow.document.getElementById('targetDiv').appendChild(HTMLpayload.cloneNode(true));
}捕获(e){
if(HTMLpayload.outerHTML){
myWindow.document.getElementById('targetDiv')。innerHTML=HTMLpayload.outerHTML;
}否则{
控制台日志(e);
}
}
我想about:blank
是在怪癖模式下运行的,它不支持svg
@Teemu它是在IE9标准模式下运行的…是的,您的主页是,但是您打开的窗口更可能在怪癖模式下运行,因为它没有doctype声明。。。只需在其中打开一个真正的文档即可进行测试。@Teemu上述代码对其他html元素也不起作用..:(我尝试用只包含文本的html div打开新窗口。即使这样也不起作用。我得到一个空白页。奇怪的是,IE抛出SCRIPT5022错误(抛出异常且未捕获
)。但此代码与try..catch
?!同时将真实页面加载到新打开的窗口不会删除此错误。设置w.document.body.innerHTML='…'
似乎有效。当封闭appendChild()时
在try..catch
中,错误消息是HierarchyRequestError
,这意味着“无法在请求的位置插入节点”。这在IE中嗅到了一个很大的错误…我尝试了这个..但我得到了这个错误:DOM异常:层次结构请求错误(3)也有这个错误的地方(请使用句子或部分代码)谢谢大家!在我附加的相同上下文中创建节点解决了问题:)
var childWindow = window.open('somepage.html');
//will throw the exception in IE
childWindow.document.body.appendChild(document.createElement('div'));
//will not throw exception in IE
childWindow.document.body.appendChild(childWindow.document.createElement('div'));
var myWindow = window.open('about:blank', 'loading...', '');
var myWindowDoc = myWindow.document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
var myWindowBody = myWindow.document.createElementNS('http://www.w3.org/1999/xhtml', 'body');
myWindow.document.open().write('<html><head></head><body><div id="targetDiv"></div></body></html>');
myWindow.document.close();
try {
myWindow.document.getElementById('targetDiv').appendChild(HTMLpayload.cloneNode(true));
} catch (e){
if (HTMLpayload.outerHTML) {
myWindow.document.getElementById('targetDiv').innerHTML = HTMLpayload.outerHTML;
} else {
console.log(e);
}
}