Redirect out of frame page to specific frame in index.htm

て烟熏妆下的殇ゞ 提交于 2019-12-02 17:53:22

问题


I am using frames on index.html file. On every page of the frame I have a code which checks if page is in frame, and if not, then redirect to index.html.

Now. I want to not only check if page is in frame and redirect to index.html, but also I want to open the page in one of the frames on index.html.

I have embedded the JS file with this code as of right now:

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

Is there any scripts you might know of?


回答1:


You need to make the code in the subpages append their name to the 'index.html', e.g.

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

...and put another Javascript on the index.html page which checks for the part after the question mark, e.g.:

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

By the way, you need to give your target frame a name like this:

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


来源:https://stackoverflow.com/questions/9514666/redirect-out-of-frame-page-to-specific-frame-in-index-htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!