2012-10-04 55 views
0

对象在Internet Explorer我的代码工作得很好,但我在Mac上使用Safari,这让我那个错误。这是我的代码:类型错误:“未定义”不是(评估“window.frames [‘内容’] document.location。”)

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

+1

请提出问题。 –

+0

你会得到“不安全的JavaScript尝试访问框架与URL”错误? – MikeB

+0

我在Safari或Chrome中没有遇到这个错误。 [小提琴](http://jsfiddle.net/barmar/cqjJZ/) – Barmar

回答

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中显示,因此一些浏览器会拒绝显示它们,而其他浏览器会在新标签页或窗口中打开它们。

相关问题