0

開いたウィンドウでスクリプトとiframe要素を記述したいのですが、FirefoxとChromeでは正常に機能しますが、IEでは機能しません。

私のコードは以下の通りです:

 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の回避策があるかどうか教えてください

4

2 に答える 2

0

それは非常に簡単です。getElementsByTagNameはIEではサポートされていません(それか、それが適切に機能しないかのどちらかです)。

このような場合は、jQueryのようなライブラリを入手することをお勧めします。これは、すべてのブラウザーで機能するコードを作成するのに役立ち、ブラウザーxまたはyで機能しない理由を理解するのに何時間もかかることはありません。

于 2011-08-11T17:38:13.047 に答える
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();
     }
 }
于 2011-08-25T06:06:10.513 に答える