Skip to content Skip to sidebar Skip to footer

TypeError: 'undefined' Is Not An Object (evaluating 'window.frames["content"].document.location')

On Internet Explorer my code works perfectly well, but I am using Safari on a Mac and it gives me that error. This is my code:

Solution 1:

That error is telling you that window.frames["content"].document resolves to undefined so the browser can't access the location property.

Accessing the document in an iFrame is different in different browsers, see below. Also, chaining references like that is not a good idea (as you've discovered) as it makes debugging harder.

function setIframeHref(iFrameID, href) {
    var frame, cont, doc;

    // Firstly, get the iframe
    frame = window.frames[iFrameID];

    // In some versions of IE, frame.document is the document the iFrame 
    // is in, not the document in the iFrame. Also, some browsers
    // use contentWindow and others contentDocument.
    // In some browsers, contentDocument points to the iFrame window,
    // in others to the document, so...
    if (frame) {
        cont = frame.contentWindow || frame.contentDocument;

        // cont might be the iFrame window or the document
        if (cont) {
            doc = cont.document || cont;

        // For current browsers that don't have the above
        } else {
            doc = frame.document
        }   
    }

    // If have a document, set the vaue of location.href
    if (doc && doc.location) { 
        doc.location.href = href;
    }
}

Note that you may still have issues if the HREF isn't from the same domain or sub–domain. Some sites don't let their pages be displayed in iFrames so some browsers will refuse to display them while others will open them in new tabs or windows.


Post a Comment for "TypeError: 'undefined' Is Not An Object (evaluating 'window.frames["content"].document.location')"