问题
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?
回答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.write
s)
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.
回答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.
来源:https://stackoverflow.com/questions/9515811/using-javascript-to-set-up-frames