我的网站上有一个包含2个iframe的父页面。目前,我已经按照自己的意愿运行,除了使用浏览器后退按钮仅更改2个iframe中的1个的内容。我正在尝试在子框架内进行检查,以便在姐妹iframe中加载正确的内容页面。我的iframe是leftiframe和rightiframe。

这是我目前拥有的代码(无法正常工作):

<script type="text/javascript">
window.onload = function() {
  if(/iframe1.html/.test(parent.leftiframe.location.href))
  {
  }
  else
  {
   parent.leftiframe.location.href="iframe1.html";
  }
}
</script>


如果我更改它,以便它从当前窗口中读取字符串,如下所示:

  if(/iframe2.html/.test(window.location.href))


那么它就可以毫无问题地读取网址,并且可以毫无问题地更改姐妹iframe的网址。为什么parent.leftiframe不能代替window?

谢谢!

最佳答案

尝试使用此代替:

<script type="text/javascript">
    window.onload = function() {
        if(/iframe1.html/.test(parent.leftiframe.contentWindow.location.href))
        {
        }
        else
        {
            parent.leftiframe.contentWindow.location.href="iframe1.html";
        }
    }
</script>

10-07 12:45