TypeError:'undefined'不是一个对象(评估'window.frames [" content&quot ;. document.location') [英] TypeError: 'undefined' is not an object (evaluating 'window.frames["content"].document.location')

查看:119
本文介绍了TypeError:'undefined'不是一个对象(评估'window.frames [" content&quot ;. document.location')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Internet Explorer上,我的代码工作得很好,但我在Mac上使用Safari,它给了我这个错误。这是我的代码:

 <!DOCTYPE html> 
< html>
< head>
< title> The Me Project< / title>
< link href =me.css =stylesheettype =text / csshref =me.css/>
< script type =text / javascript>
函数页(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-Compatiblecontent =IE = edge,chrome = 1>
< / head>
< body>
< div id =header>
< div id =nav>
< h1>帕特的我项目< / h1>
< ul>
< li>< a onclick =page('home')>主页< / a>< / li>
< li>< a onclick =page('birth')>出生年度< / a>< / li>
< li>< a onclick =page('cool')>酷玩< / a>< / li>
< li>< a onclick =page('family')>我的家庭< / a>< / li>
< / ul>
< / div>
< / div>
< div id =main>
< iframe src =home.htmlname =contentid =contentseamless height =1000frameborder =0/>
< / div>
< frame>
< / body>
< / html>

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

解决方案

这个错误告诉你 window.frames [content]。document 解析为 undefined ,因此浏览器无法访问位置属性。

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

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

//首先,获取iframe
frame = window.frames [iFrameID];

//在某些版本的IE中,frame.document是iFrame
所在的文档,而不是iFrame中的文档。此外,某些浏览器
//使用contentWindow和其他contentDocument。
//在某些浏览器中,contentDocument指向iFrame窗口,
//在其他文档中,所以...
if(frame){
cont = frame。 contentWindow || frame.contentDocument;

// cont可能是iFrame窗口或文档
if(cont){
doc = cont.document ||续;

//对于没有上述
的当前浏览器} else {
doc = frame.document
}
}

//如果有文档,请设置location.href
的值,如果(doc&& doc.location){
doc.location.href = href;


$ / code>

请注意,如果HREF不是来自同一个域或子域。有些网站不会让他们的网页在iFrame中显示,因此一些浏览器会拒绝显示它们,而其他浏览器则会在新标签页或窗口中打开它们。


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.

解决方案

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.

这篇关于TypeError:'undefined'不是一个对象(评估'window.frames [&quot; content&quot ;. document.location')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆