【问题标题】:Using JavaScript to set up frames使用 JavaScript 设置框架
【发布时间】:2012-03-01 12:00:09
【问题描述】:

我将任何不在框架中的页面重定向到 index.html。索引正在更改为 index.html?page.html

我想使用地址中的附加部分并将帧的 SRC 设置为该值。

我不知道如何正确包含location.search.substring(1),以免导致错误。

每个站点都有代码:

if (top.location == self.location)
{
    top.location = 'index.html?' + location.href.replace(/^.*[\\\/]/, '');
}

索引页包含在

之后
<frameset rows="100px,100%" cols="*" border="0">
    <frame src="logo.html" name="logo" scrolling="no">
    <frameset rows="*" cols="200,100%" border="0">
        <frame src="menu.html" name="menu" scrolling="no">
        <script language="javascript">
            if (location.search && location.search.length > 1 && location.search.charAt(0) == "?") 
            {
                document.write('<frame src="' + location.search.substring(1) + '" name="page" scrolling="auto">');
            }
            else
            {
                document.write('<frame src="main_page.html" name="page" scrolling="auto">');
            }
        </script>
    </frameset>
</frameset>

这个想法是从我之前的question得到的。

顺便说一句。我应该这样做吗?有没有更好的解决方案?

【问题讨论】:

  • location 是每一帧中的一个全局对象 (window-object)。它应该被称为:top.window.locationwindow.locationlocation 本身并不代表页面的当前 URL,如果尚未重定向,则可以从名为 href 的属性中找到该 URL。 href也用于更改页面的URL。
  • @Teemu,我明白你在说什么,但我不知道如何让它发挥作用。

标签: javascript html frames


【解决方案1】:

如果你想使用框架,这个想法是可以的。旨在显示在框架中的孤独页面通常需要来自其他框架或 top.window 的内容。工作解决方案:

1) 不需要写入frameset-tags 的脚本。

2) 重定向页面:

if(top.window.location.pathname == self.window.location.pathname){ // Compares without search string
    top.window.location.href = 'your_page_address+'?current_page_name'
}

3) 创建一个假页面以加载到page-frame 并在那里放置一个脚本,该脚本检索 top.window 的 URL。然后将虚假页面重定向到用户之前所在的页面。如果原始页面是您的 index.html,则只需加载正确的页面。 该脚本也可以放在menu-frame 中的文档中,但是您必须等到所有帧都完全加载后才能重定向。

请注意,您将从location 对象的属性中获得的结果因浏览器而异。因此,请避免对 URL 进行文字比较和操作,仅使用 location-object 的属性。

【讨论】:

  • 你介意告诉我这是怎么做的吗?我在 js 中有点空白,但我需要这个脚本,因为我在我的页面上使用框架。我的第一个脚本很好地重定向到主页并添加页面名称:index.html?about.html。问题是我不知道如何从索引的地址栏中获取该信息并使用它来加载页面框架。有什么想法吗?
【解决方案2】:

好的,这是一个简单的例子。我认为存在许多更复杂的解决方案,但该示例包含任务的所有基本内容。也不需要虚假页面。

我仅在本地对此进行了测试,Chrome 仅重定向到首页。其他浏览器(FF、IE、Opera)按预期工作。

1) 将你的 index.html 重写为普通框架集(即:没有document.writes)

2) 将此脚本放在index.html的head

reDirectCleared=false;

3) 将此脚本放在首页的head

(function reDirect(){
    // ** Redirect if no frameset
    if(top.window.location.pathname == self.window.location.pathname){
        top.window.location.href = 'Your_index.html_URL?This_page_URL&'
    }
    // ** Frameset exist
    if(!top.window.reDirectCleared){
        top.window.reDirectCleared=true; // * Prevents further redirections
        var searchString = top.window.location.search;
        var pagePath = searchString.substring(1,searchString.indexOf('&'));
        if(pagePath.indexOf(self.window.location.pathname)>-1 || pagePath==''){
            return; // * Ready at frontpage
        }
        top.window.page.location.href = pagePath; // * Redirects page-frame
    }
    return;
})();

4) 将此脚本放在所有其他页面的head 中(不在index.html 中)

if(top.window.location.pathname == self.window.location.pathname){
    top.window.location.href = 'Your_index.html_URL?This_page_URL&'
}

你的功课是,如果 Chrome 无法使用真正的 http 页面,如何让 Chrome 正确执行此操作。

【讨论】:

  • 哇哦!这非常适合它需要做的事情。非常感谢!
  • 正如您所说,chrome 似乎无法完成这项工作,但 firefox,即,opera 和 safari 可以很好地完成这项工作。只要此脚本适用于大多数网络浏览器,我就可以了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多