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解決されることを示しています。undefinedlocation

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 に答える