js实现iframe高度的自适应

function autoSetIframeHeight(id) {
  try{
        var iframe = document.getElementById(id);
        if(iframe.attachEvent){
          window.setTimeout(function() {
      
  console.log(iframe.contentWindow.document.documentElement.scrollHeight)
         iframe.style.height =  (iframe.contentWindow.document.documentElement.scrollHeight+100)+'px';
      },500)

            iframe.attachEvent("onload", function(){
            iframe.contentWindow.onclick = function () {
              autoSetIframeHeight(id);
             }
            });
            return;
        }else{
          window.setTimeout(function() {
        console.log(iframe.contentDocument.body.scrollHeight)
         iframe.style.height =  (iframe.contentDocument.body.scrollHeight+100)+'px';
      },500)
            iframe.onload = function(){
               iframe.contentDocument.onclick = function () {
              autoSetIframeHeight(id);
           }
            };
            return;                 
        }     
    }catch(e){
        throw new Error('setIframeHeight Error');
    }
}

你可能感兴趣的:(js实现iframe高度的自适应)