TypeError: 'undefined' is not an object (e

2019-09-09 19:09发布

问题:

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:

<!DOCTYPE html>
<html>
<head>
<title>The Me Project</title>
<link href="me.css" rel="stylesheet" type="text/css" href="me.css" />
<script type="text/javascript">
function page(setter)
{
    if (setter=="home") 
    {
        window.frames["content"].document.location.href = "home.html";
    }
    else if (setter=="birth")
    {
        window.frames["content"].document.location.href = "birth.html";
    }
    else if (setter=="cool")
    {
        window.frames["content"].document.location.href = "cool.html";
    }
    else if (setter=="family")
    {
        window.frames["content"].document.location.href = "family.html";
    }
    else
    {
        window.frames["content"].document.location.href = "home.html";
    }
}
</script>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
</head>
<body>
<div id="header">
    <div id="nav">
    <h1>Parth's Me Project</h1>
    <ul>
    <li><a onclick="page('home')">Home</a></li>
    <li><a onclick="page('birth')">Birth Year</a></li>
    <li><a onclick="page('cool')">Cool Things</a></li>
    <li><a onclick="page('family')">My Family</a></li>
    </ul>
    </div>
</div>
<div id="main">
<iframe src="home.html" name="content" id="content" seamless height="1000" frameborder="0"/>
</div>
<frame>
</body>
</html>

If you need the other pages, just tell me.I am trying to keep the same header and nav bar, so I am using an iframe in the bottom section.

回答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.