2012-07-08 48 views
1

我動態創建一個iframe:如何關閉使用JavaScript滾動的iframe?

function iFrame(parentElement) 
    { 
    var iframe = document.createElement("iframe"); 
    if (parentElement == null) 
    { 
     parentElement = document.body; 
    } 
    parentElement.appendChild(iframe); 
    iframe.doc = null; 
    if (iframe.contentDocument) 
    { 
     iframe.doc = iframe.contentDocument;   // Opera, Firefox 
    } 
    else if(iframe.contentWindow) 
    { 
     iframe.doc = iframe.contentWindow.document; // Internet Explorer 
    } 
    else if(iframe.document) 
    { 
     iframe.doc = iframe.document; 
    } 
    if(iframe.doc == null) 
    { 
     throw "Application failed to dynamically create an iframe content"; 
    } 
    iframe.doc.open(); 
    iframe.doc.close(); 
    return iframe; 
    } 

...與document.onload:

function onPageLoad() 
    { 
    var iframeDiv = document.getElementById("iframeDiv"); 
    var iframe = new iFrame(iframeDiv); 
    } 

我怎麼能伸展此iframe到其父的div和關閉使用JavaScript滾動?

回答

3

您可以通過設置關閉在iframe滾動:

iframe.style.overflow = "hidden" 

如果父div有一個定義的高度和寬度,然後就可以設置的iframe有100%的高度和寬度:

iframe.style.width = iframe.style.height = "100%"; 

更多的信息在this previous SO question/answer