开发者

Javascript to insert IFRAME

开发者 https://www.devze.com 2022-12-21 02:10 出处:网络
I\'d like to give people the ability to insert a single Javascript line into their si开发者_Go百科te which effectively allows me to insert an IFRAME of a fixed size that contains content from my site.

I'd like to give people the ability to insert a single Javascript line into their si开发者_Go百科te 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?


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号