2011-08-11 58 views
0

我想在打開的窗口中編寫腳本和iframe元素,在Firefox和Chrome中可以正常工作,但在IE中不能。在IE中插入腳本和iframe標記

我的代碼如下:

var mywindow = window.open(url,'child',params); 
mywindow.onload = writeToWindow; 

function writeToWindow(){ 
     var myScriptEle = document.createElement('script'); 
     myScriptEle.type = "text/javascript"; 
     myScriptEle.text = " some script"; 

     var myIframe = document.createElement("IFRAME"); 
     myIframe.id = "childwindow"; 
     myIframe.name = "childwindowname"; 
     myIframe.src = MyframeUrl; 

     if(mywindow){ 
     mywindow.document.getElementsByTagName('body')[0].appendChild(myScriptEle); 
      mywindow.document.body.appendChild(myIframe); 
     } 
} 

遇到錯誤

在此先感謝「支持沒有這樣的接口」,請讓我知道,如果有任何變通的IE

回答

0
var mywindow = window.open(url,'child',params); 
writeToWindow(); 

function writeToWindow(){ 
    var content = '<script type="text/javascript">'; 
    content += 'function updatePage(){ '; 
    content += 'var myiframe = document.createElement("IFRAME");'; 
    content += 'myiframe.id = "myIframe";'; 
    content += 'myiframe.name = "iframe_js";'; 
    content += 'myiframe.className = "iframe_js";'; 
    content += 'myiframe.src ="http://stackoverflow.com"'; 
    content += '</script>'; 
    if(mywindow){ 
      mywindow.document.open(); 
      mywindow.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'+content+'</head><body onload="updatePage()"><div></div></body></html>'); 
      mywindow.document.close(); 
    } 
} 
0

那麼這很容易,getElementsByTagName在IE中不受支持(或者它或者它不應該做它應該做的)!

對於這樣的事情,你最好得到像jQuery這樣的庫。這將幫助你編寫代碼,將在所有的瀏覽器,並不會花費你的時間小時搞清楚爲什麼東西並不在瀏覽器x或y工作

+0

感謝您的回覆,是否有任何工作用Jquery出去用? – kkchaitu