0

在 Internet Explorer 上,我的代码运行良好,但我在 Mac 上使用 Safari,它给了我这个错误。这是我的代码:

<!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>

如果您需要其他页面,请告诉我。我试图保持相同的标题和导航栏,所以我在底部使用 iframe。

4

1 回答 1

2

该错误告诉您 window.frames["content"].document解析为,undefined因此浏览器无法访问该location属性。

在 iFrame 中访问文档在不同的浏览器中是不同的,见下文。此外,像这样链接引用不是一个好主意(正如您所发现的那样),因为它使调试变得更加困难。

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;
    }
}

请注意,如果 HREF 不是来自同一个域或子域,您可能仍然会遇到问题。一些网站不允许他们的页面显示在 iFrame 中,因此一些浏览器会拒绝显示它们,而另一些则会在新选项卡或窗口中打开它们。

于 2012-10-04T01:55:26.813 回答