Forum Moderators: open

Message Too Old, No Replies

Complex Frame Breakout

         

mixermanic

1:35 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



Hi everyone,

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

Bernard Marx

3:51 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

Stratus42

4:42 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



I do something very similar to your first problem on my pages .. if anybody accesses one of my main pages by themselves, without their frame sets.. this sets them straight:

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

Bernard Marx

5:35 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, Stratus42. There's nothing wrong with that, but there's a couple of things extras needed for mixermanic.

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.

mixermanic

9:16 am on Apr 16, 2004 (gmt 0)

10+ Year Member



Brilliant thanks everyone! :)

Can you have a function checking for the correct domain? Then I could check, for example, that the page is in the correct frameset by having something like:

if{ top!= 'mydomain.com' }

(bad and wrong script I know but it serves to explain what I mean :p )

Thanks

Bernard Marx

5:10 pm on Apr 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



checkout the location.hostname property (It'll be blank if you run it from the HD).

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.