2011-11-17 53 views
-5

如何調整外部網站以適應我的網站中的iframe? 請任何人幫助我?如何調整外部網站以適應iframe?

+1

更具體。 PHP和iframe有什麼關係? –

+1

你的意思是一個具有「特定」高度的iframe? http://stackoverflow.com/questions/7072681/disable-iframe-auto-resize – Zhianc

+1

我會認真勸告iframes。煩人的縮影。 – tekknolagi

回答

0

你不能與外部交互的iframe,如果它是在不同的領域。

/** 
* this function allows you to retrieve the window object of an iframe given by its ID 
* 
* @param {string} id - iframe id 
*/ 
var getIframe = function (id) { 
    if ($.browser.mozilla) return document.getElementById(id).contentWindow; 
    return window.frames[id]; 
}; 

然後,從外部文件(其中包含的iframe的),你可以調用像這樣被加載的iframe時:如果是在同一個域中,你可以按如下調整它的內容:

var resizeIframeContent = function(iframeID, sizeX, sizeY){ 

    var iframe = getIframe(iframeID); 
    iframe.updateSize(sizeX, sizeY); 

} 

有了這個代碼,您呼叫的iframe的內部文件中的函數updateSize,所以你應該附加到window對象(全局命名空間)嵌套文檔中添加此功能。事情是這樣的:

var updateSize= function(sizeX, sizeY){ 
    //do you resizing stuff here, which depends on your html structure, it could be something like this: 
    $("#content").width(sizeX).height(sizeY); 
} 

不過,我認爲你的問題是錯誤的,你實際上是試圖問你怎麼可以修改你的iframe的大小(而非iframe中的內容),以配合其內部的內容。如果是這樣,你不能在跨域的情況下做,但如果兩個頁面都在同一個域中,你可以以類似的方式做到這一點,如下所示:

在內部文檔上,當文檔準備就緒時,發送其大小以父容器:

$(function(){ 
    var content = $("#content"); //change this to point your container, or obtain the document height or anything else 
    parent.onIframeDimensionsRetrieved(content.width(), content.width()); 
}); 

和你outter文檔(一個使用iframe)檢索新的大小和更新的iframe尺寸:

var onIframeDimensionsRetrieved = function(width ,height){ 
    $("#youriframe").width(width).height(height); 
} 

這是所有一切:-)我的示例代碼使用jQuery來使其可讀,如果你使用純JS,你也可以使用它你想要。

祝你好運!我希望它可以幫助:-)