submitted by: Guest

Javascript: Return the document of an IFrame in any browser

Reliably gain access to the "document" Object within the IFrame of your choice.


code snippet:
// Return IFrame's Document
returnIFDoc = function(ifr){
        //      For IE5.5 and IE6                        For NS6
        var ifdoc = ifr.contentWindow || ifr.contentDocument;
        if(ifdoc){
            if (ifdoc.document) {
                return ifdoc.document;
                }else{
                        return ifdoc;
                }
        }else{
             return ifr.document;
        }
};

sample output:
The "document" Object within the specified IFrame. Regular DOM functions that operate on the "window.document" Object will also work on this object.

notes:
Compatible to all browers, tested with IE 7, Firefox 3, Opera 9, Safari 4.