Javascript to insert IFRAME

廉价感情. 提交于 2020-01-17 08:47:39

问题


I'd like to give people the ability to insert a single Javascript line into their site which effectively allows me to insert an IFRAME of a fixed size that contains content from my site. It's effectively a widget which allows them to search my site or receive other information. Is this possible?


回答1:


Yes, it is possible.
You can use document.createElement("iframe") and then appendChild() or replaceChild() the new element =/ [if you use replaceChild, you define a dummy div that has the width/height of your iframe]

Or am I interpreting your question incorrectly?




回答2:


Here is a suedo code for guaranteed insertion and loading of iframe

var __IE__ = navigator.userAgent.indexOf("MSIE") >= 0 ;
var iframe=document.createElement("iframe");
iframe.src="__URL__";

if(document.reayState === "complete" || ( __IE__ && document.readyState === "interactive" )){

       document.body.appendChild(iframe);

}else{          
      if(__IE__){
         document.onreadystatechange = function(){
           if(document.readyState === "complete"){
                document.onreadystatechange = null;
                document.body.appendChild(iframe);
           }
         };
      }else{
         var callback = function(){
              if(document.removeEventListener){
                   document.removeEventListener("DOMContentLoaded",callback,false);
              }
              window.onload = function(){};
              document.body.appendChild(iframe);
         }

         if(document.addEventListener){
            document.addEventListener("DOMContentLoaded",callback,false);
         }
         window.onload = callback;


      }

}

If document state is loading prefer document.write , else you can use above code.



来源:https://stackoverflow.com/questions/2337010/javascript-to-insert-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!