3

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

http://davidwalsh.name/iframe-permission-denied

https://sqa.stackexchange.com/questions/1453/how-to-fix-permission-denied-to-access-property-document

http://sahi.co.in/forums/discussion/2898/error-permission-denied-to-access-property-document-with-ckeditor/p1

http://www.codingforums.com/showthread.php?t=236484

Community
  • 1
  • 1
Soumya
  • 425
  • 1
  • 6
  • 28
  • 1
    which string error referred to ? `iFrames[i].contentWindow.document.` ? I think you have foreign iframes on the page, made by some social network plugins or like that, best for you is to apply to own iFrames special class and use `document.getElementsByClassName()` – zb' Feb 23 '13 at 04:15
  • Will you please provide me an example. I am very poor at Jquery. Though there should not be any foreign Iframe as no Social Network Plugin were used in the site I made. I am getting errors on the contentWindow.document part mainly. So I used the 2nd script resizeIframe(ifRef). But still didn't work. As I said the 1st one was working fine before. It stopped working after domain transfer – Soumya Feb 23 '13 at 04:25
  • I just used the way said and again got Permission denied to access property 'document' error – Soumya Feb 23 '13 at 04:32
  • check the `console.warn('SRC',iFrames[i].src); ` (add it before line that gives error); open js console and wathc – zb' Feb 23 '13 at 04:56
  • The error is not coming on `iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';` part but rather on the part `this.style.height = this.contentWindow.document.body.offsetHeight + 'px';`; – Soumya Feb 23 '13 at 04:58
  • When I am using resizeIframe(ifRef) and alerting ifdoc then I am getting undefined. – Soumya Feb 23 '13 at 05:10
  • ok than `console.warn(this.src)`; – zb' Feb 23 '13 at 05:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25000/discussion-between-soumya-and-eicto) – Soumya Feb 23 '13 at 05:38
  • yes I have tried that also but still not working. I am somehow confused what to do to solve it. And according the Cross Server Scripting I am getting Permission denied to access property document or Permission denied to access property documentElement. So I think Iframe may not able to solve the problem so I probably have to use ajax. – Soumya Feb 23 '13 at 05:41

1 Answers1

-2

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.

cliffbarnes
  • 1,396
  • 11
  • 21
  • yes you are right. Then I probably have no option else rather than using ajax instead of Iframe. Or else you can give me any alternative idea of getting the height of Iframe parents without `contentWindow.document.documentElement` or `contentDocument.documentElement` – Soumya Feb 23 '13 at 05:42
  • 1
    ajax calls to a cross domain won't work either :P ... unless you make the call to a server side script that makes the XSS call or you have a JSONp hook. – cliffbarnes Feb 23 '13 at 05:48
  • Then what will be the solution? Will you please guide me? – Soumya Feb 23 '13 at 05:49
  • This is a security feature of all modern browsers. You are not allowed to do this. – cliffbarnes Feb 23 '13 at 05:50
  • then how can anybody implement these kind of stuff? – Soumya Feb 23 '13 at 09:24
  • Yes By Ajax you will able to implement this. – Jhilom Feb 23 '13 at 19:10
  • @cliffbarnes This answer is only partly correct. You **CAN** manipulate window contents of iframes pointing to external domains **IF** you have control over it. See http://easyxdm.net/wp/ – Noz Mar 20 '13 at 17:13
  • @Cyle ... and what part of his explanation did he clue you to that? Even so, that answer will only work for certain cases. Too bad you didn't put your answer out there ... instead of just hating. But I suspect you knew what the result would be. – cliffbarnes Mar 27 '13 at 07:26
  • What part of who's explanation? I know it will only work in certain cases, hence the bolded **IF** in my comment. I'm not sure what part of my comment can be interpreted as hating..? I didn't post this as an answer because I feel half-assed answers that simply link to other articles that can explain something much better than you could aren't worth reputation. The reason I left this comment on your answer in particular is because you made it out to be something that can't ever be done. – Noz Mar 27 '13 at 18:17
  • And if you control the cross-domain you can make the AJAX call...But its obvious from the context he/she doesn't, if you understand English. But the point is you didn't post an answer, you just post hair-brained reasons why the sky isn't really blue. POST YOUR ANSWER. But you don't have one tho, do you? Just some article online 'you think' is a single exception to what I said....wow – cliffbarnes Apr 09 '13 at 02:52