Iframe から呼び出されたページの高さを取得する際に、非常に奇妙なエラーが発生します。私はグーグルに行き、この「プロパティドキュメントへのアクセス許可が拒否されました」に関連する他のStackoverflowの投稿もチェックしましたが、まだ解決策が見つかりませんでした. 別のサーバーを指している Web サイトがあります。そして、Iframeでこのエラーが発生しています。コードを提供させてください
Jクエリ
$(document).ready(function()
{
// Set specific variable to represent all iframe tags.
var iFrames = document.getElementsByTagName('iframe');
// Resize heights.
function iResize()
{
// Iterate through all iframes in the page.
for (var i = 0, j = iFrames.length; i < j; i++)
{
// Set inline style to equal the body height of the iframed content.
iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
}
}
// Check if browser is Safari or Opera.
if ($.browser.safari || $.browser.opera)
{
// Start timer when loaded.
$('iframe').load(function()
{
setTimeout(iResize, 0);
}
);
// Safari and Opera need a kick-start.
for (var i = 0, j = iFrames.length; i < j; i++)
{
var iSource = iFrames[i].src;
iFrames[i].src = '';
iFrames[i].src = iSource;
}
}
else
{
// For other good browsers.
$('iframe').load(function()
{
// Set inline style to equal the body height of the iframed content.
this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
}
);
}
}
);
そしてIFrame
<iframe style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe>
「プロパティ ドキュメントにアクセスする権限が拒否されました」というエラーが発生していたため、高さがドキュメントの高さに応じて表示されません。次に、次のコードを試しました
function resizeIframe(ifRef)
{
var ifDoc;
//alert(ifRef);
try
{
ifDoc = ifRef.contentWindow.document.documentElement;
}
catch( e )
{
try
{
ifDoc = ifRef.contentDocument.documentElement;
}
catch( ee ){}
}
var doc = ifRef.height;
//alert(doc);
if(ifDoc)
{
ifRef.height = 1;
ifRef.style.height = ifDoc.scrollHeight+'px';
}
}
そしてiframe
<iframe onload="resizeIframe(this)" style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe`>
この場合、javascript でエラーは見つかりませんでしたが、機能しませんでした。しかし、奇妙なことに、両方とも私のローカルサーバーで動作しており、コードが現在存在するサーバーが別のサーバーを指していないときにも動作していました.(クロスサイトスクリプティング)。
これを解決する方法を教えてください。
私が使った参考書
エラー: プロパティ 'ドキュメント' へのアクセスが拒否されました
エラー document.form is undefined in javascript