views:

44

answers:

2

I've got this code throwing an error from an iframe:

            function parentIframeResize()
        {
            var height = getParam('height');
            // This works as our parent's parent is on our domain..
            parent.parent.resizeIframe(height);
        }

Not concerned about the error at all. The problem is it stops other scripts from running. Firefox, chrome or any decent browser just keeps running the rest of the scripts. I need to suppress the error or make sure parent.parent exists before running the code.

In php I would write something like if(!empty(parent.parent) { //do stuff with parent.parent } to check if the object exists.

Yes, nested iframes is ugly O_o

+3  A: 

Try,

if (parent.parent && parent.parent.resizeIframe) {
    // parent.parent exists and supports resizeIframe
    parent.parent.resizeIframe(height);
}

That should work and stop the errors.

Nick Presta
thomasrutter
much more elegant than try / catch, esp with thomasrutter's suggestion. +1 to both
seanizer
+2  A: 

You could wrap it up in a try/catch block:

function parentIframeResize() {
    try {
        var height = getParam('height');
        parent.parent.resizeIframe(height);
    } catch(err) {
        // do something to recover from the problem, or nothing to suppress it
    }
}
Pat