0

I am redirecting any page that is not in frame to index.html. The index is being changed to index.html?page.html

I want to use the additional part from address and set the SRC of the frame to this value.

I don't know how to include location.search.substring(1) correctly so it wouldn't cause errors.

Each site has code:

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

The index page contains right after

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

I got this idea from my previous question.

BTW. Should I even do this? Is there any better solution?

Community
  • 1
  • 1
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • `location` is an global object in each frame (`window`-object). It should be refered like: `top.window.location` or `window.location`. `location` itself does not represent the present URL of the page, that can be found from it's property named `href`, if not already redirected. `href` is also used to change the URL of the page. – Teemu Mar 01 '12 at 12:07
  • @Teemu, I see what you are saying, but I have no idea how to make it work. – HelpNeeder Mar 01 '12 at 12:23

2 Answers2

2

The idea is allright, if you want to use frames. Lonely pages purposed to be shown in the frames often need something from other frames or top.window. Working solution:

1) No script into frameset-tags is needed.

2) Redirect the page:

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

3) create a fake page to load to page-frame and put there a script, which retrieves the URL of the top.window. Then redirect the fake page to the page where user earlier was. If the original page was your index.html, then just load the right page. That script can also be put to the document in menu-frame, but then you have to wait untill all frames are fully loaded before redirecting.

Notice, results what you will get from location object's properties, varies depended on browser. Hence avoid literal comparing and manipulating of the URLs, use location-object's properties only.

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • Do you mind telling me how this is done? I'm kind of blank in js but I need this script because I'm using frames on my page. My first script does a nice job to redirect to main page and add page name as such: `index.html?about.html`. The problem is that I don't know how to grab that information from the address bar from index and use it to load in the page frame. Any ideas? – HelpNeeder Mar 01 '12 at 23:10
1

OK, here is a simple example. I think a lot of more sophisticated solutions exist, but the example contains all basic stuff for a task. A fake page is not needed also.

I've tested this locally only, and Chrome redirects just to the frontpage. Other browsers (FF, IE, Opera) work as expected.

1) Rewrite your index.html as normal frameset (that is: no document.writes)

2) Put this script in the head of index.html

reDirectCleared=false;

3) Put this script in the head of your frontpage

(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) Put this script in the head of all other pages (not in index.html)

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

Your homework is, how to get Chrome to do this right, if it fails with real http-pages.

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • Oh wow! This is perfect for what it needed to do. Thanks a lot! – HelpNeeder Mar 02 '12 at 20:31
  • As you said, chrome doesn't seem to make this work, but firefox, ie, opera, and safari does the job well. As long this script works on majority of web browsers, I am fine with it :) – HelpNeeder Mar 02 '12 at 20:35