繁体   English   中英

在 IE 中插入脚本和 iframe 标签

[英]Insert script and iframe tag in IE

我想在打开的 window 中编写脚本和 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有任何解决方法,请告诉我

嗯,这很容易,在 IE 中不支持 getElementsByTagName(好吧,或者它没有做它应该做的)!

对于这样的事情,你最好得到一个像 jQuery 这样的库。 这将帮助您编写适用于所有浏览器的代码,并且不会花费您数小时的时间来弄清楚为什么某些东西在浏览器 x 或 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();
     }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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