Forum Moderators: open
I have a site with 3 frames: "topframe", "sideframe", and "mainframe".
mainframe is populated with pages in the format "main.php?page=pagename" (all the pages are served as main.php).
I am trying - with absolutely no success whatsoever - to write a javascript that I can link to on every page, where if any of the pages (index.php, sidenav.php or topnav.php) are served outside of the frameset, it will automatically redirect to load within the frameset (called index.html) WITH THE CORRECT PAGE LOADED IN THE MAINFRAME.
So if "main.php?page=contact" is opened, it will load the frameset correctly and also put "main.php?page=contact" into the mainframe.
I would also like to make sure that if any pages are loaded in a different frameset, it breaks out to my frameset.
I hope this makes sense! Many thanks in advance,
Martin
So if "main.php?page=contact" is opened, it will load the frameset correctly and also put "main.php?page=contact" into the mainframe.
This can be done with JavaScript, but it would involve the mainframe being redirected after load, and affecting history (either that or we enter tricky territory).
Really, this part of the problem is suited to PHP. You want to serve the page with the main frame's src as 'contact.htm' from the start. This involves getting the search part of the url + ".htm", and then serving the page with the right mainframe src.
..at least I suppose so, as I don't know much PHP. I'd still hazard to guess that it's not so hard.
I would also like to make sure that if any pages are loaded in a different frameset, it breaks out to my frameset.
This is JavaScript territory. The classic form is to check whether the 'top.location' is what it should be ie your main page url. If it isn't, we change the page location to that.
This needs a little addition to put in the search part - for some pages at least. For this, we need to grab the filename, minus the extension.
Right now, the only way I can think of getting this is to get whatever's between the last '/', and the last '.' in the url.
Altogether now..
var mainURL = [enter main page url]
if(top.location.href!= mainURL)
{
var loc = location.href
var pagename = loc.substring(loc.lastIndexOf('\/')+1,loc.lastIndexOf('.'))
location.href = mainURL + "?pagename=" + pagename
}
..I think. Try it.
function putInFrame()
{
var strPath = location.pathname;
var strPage = strPath.substring(strPath.lastIndexOf('/') + 1);
if (top == self)
self.location.replace('index.htm?p=' + strPage);
}
seems to do the trick for me :-)
1. His format needs the page extension removed.
2. The test condition in your sample won't activate the location change if the page is in a frame (but the wrong one). I admit that's unlikely, but still worth checking for.
for you: top.location.hostname == "www.whatever.com"
Extra tip. For hunting down useful properties, pick a browser / document object and run this:
[pre][color=blue]
// say..var obj = location
var str = ''
for(var prop in obj)
str+=prop +": "+ obj[prop]+"\n"
alert(str)
[/color][/pre]
Not all properties will be cross-browser.