将框架页面重定向到index.htm中的特定框架

问题描述:

我在index.html文件上使用框架。在框架的每一页上,我都有一个代码检查页面是否在框架中,如果没有,则重定向到index.html。将框架页面重定向到index.htm中的特定框架

现在。我不仅要检查页面是否在框架中并重定向到index.html,而且还想在index.html中的一个框架中打开页面。

我已经嵌入该代码JS文件截至目前:

if (top.location == self.location) 
{ 
    top.location = 'index.html' 
} 

有没有你可能知道的任何脚本?

您需要让子页面中的代码将其名称附加到'index.html'中,例如

if (top.location == self.location) { 
    top.location = 'index.html?' + location.href; 
} 

...并把另一个JavaScript index.html页面将问号后检查的一部分,例如在:

<script type="text/javascript"> 
window.onload = function() { 
    //check if a query string is given 
    if (location.search && location.search.length > 1 
     && location.search.charAt(0) == "?") { 

     //load the URL to the frame 
     window.frames["myframe"].location.href = location.search.substring(1); 
    } 
} 
</script> 

顺便说一句,你需要给你的目标框架一个名字是这样的:

<frameset ... 
    <frame name="myframe" ... 
+0

我不能让它工作:/这让我感动,但在地址栏中的地址发生变化,以:'http://192.168.0.2/csharp%20projects/index.html?http :// 192.168.0.2/csharp%20projects/sitemap.html' from:'http://192.168.0.2/csha rp%20projects /' – HelpNeeder 2012-03-01 11:22:13

+0

它更改地址以告知框架集页面应加载哪个子页面。我认为你不能避免地址改变。这是为什么许多人不喜欢框架集的限制。 – 2012-03-03 18:23:25

+1

有趣的答案:[为什么我不应该使用HTML框架?](http://stackoverflow.com/questions/1203068/why-should-i-not-use-html-frames);-) – 2012-03-03 18:23:53