admin管理员组

文章数量:1391925

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.

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.

Share Improve this question asked Oct 4, 2012 at 0:00 Plato2000Plato2000 231 gold badge1 silver badge5 bronze badges 2
  • are you getting "unsafe javascript attempt to access frame with url" error? – MikeB Commented Oct 4, 2012 at 0:18
  • I don't get that error in Safari or Chrome. fiddle – Barmar Commented Oct 4, 2012 at 0:39
Add a ment  | 

1 Answer 1

Reset to default 3

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.

本文标签: