【问题标题】:Redirect out of frame page to specific frame in index.htm将框架页面外重定向到 index.htm 中的特定框架
【发布时间】:2012-03-19 20:33:24
【问题描述】:

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

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

截至目前,我已经用这段代码嵌入了 JS 文件:

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

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

【问题讨论】:

    标签: javascript html frame frameset


    【解决方案1】:

    您需要使子页面中的代码将其名称附加到'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" ...
    

    【讨论】:

    • 我无法让它工作:/ 它移动了我,但将地址栏中的地址更改为:http://192.168.0.2/csharp%20projects/index.html?http://192.168.0.2/csharp%20projects/sitemap.html from:http://192.168.0.2/csharp%20projects/
    • 它改变地址来告诉框架集页面它应该加载哪个子页面。我认为您无法避免地址更改。这是许多人不喜欢框架集的一个限制。
    • 有趣的答案:Why should I not use HTML frames? ;-)
    • 是的,我很乐意使用 HTML 和 JavaScript 以外的东西。但我正在做课堂项目,我的智慧有限。但是框架的积极方面是它在许多浏览器中都受支持,并且使用它并没有真正的问题。在我不得不克服重定向问题之前,一切工作人员都很完美。这个nswer 让我走上了正轨:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2010-11-17
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多