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

时间:2012-03-01 10:38:35

标签: javascript html frame frameset

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

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

我已经使用此代码嵌入了JS文件:

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

您可能知道任何脚本吗?

1 个答案:

答案 0 :(得分:2)

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

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

...并在index.html页面上放置另一个Javascript,检查问号后面的部分,例如:

<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" ...