简体   繁体   中英

Insert script and iframe tag in IE

I want to write script and iframe elements in the opened window, which works fine in Firefox and Chrome, but not in IE.

My code is below:

 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);
      }
 }

I'm getting "No such interface supported" errors

Thanks in advance, please let me know if there's any work around for IE

Well it's quite easy, getElementsByTagName is not supported(well either that or it doesnt do what it should) in IE!

For things like this you're better off getting a library like jQuery. This will help you write code that will work on all browsers and won't cost you hours of your time figuring out why stuff doesn't work in browser x or y

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();
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM