I am getting very strange error on fetching height of page called from Iframe. I went throgh google and also checked other Stackoverflow post related to this ‘Permission denied to access property document’ but did not find any solution yet. I have a website which is pointing another server. And I am getting this error on Iframe. Let me provide the code
Jquery
$(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';
}
);
}
}
);
And IFrame
<iframe style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe>
I was getting ‘Permission denied to access property document’ error so the height is not coming according to the document height in it. Then I tried the following code
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';
}
}
And Iframe
<iframe onload="resizeIframe(this)" style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe`>
In this case I found no error on javascript but it did not work. But the strange thing both of them are working on my local server and also was working when the server where the code is present now was not pointing another.(Cross Site Scripting).
Please help me how to solve this.
references I used
error : Permission denied to access property ‘document’
Error document.form is undefined in javascript
This is called an XSS exception. The error: error : Permission denied to access property ‘document’
Unfortunately, what you want to do — manipulate the contents or window of an iframe on a site different from the domain of the parent — is not allowed. Period.