You are here: irt.org | FAQ | JavaScript | Frame | Q445 [ previous next ]
Take a look at the article Re-directing access within Frames.
The following was submitted by Carsten Bernstein
Our website uses frames and PHP. Some searchengines, other websites or bookmarks link to single pages of our site - sometimes not to the full frameset.
So I needed a script to catch the frameset. Unfortunately the scripts I found added the requested URI to the frameset. Ex: index.html?pageIWantToSee.php?id=3&whatEver=all
With those scripts it was impossible to use the "Open in a new window"-feature of a browser, so I made something new based on Q445. Attention: It´s simple, it works and suits my needs though it is far from perfection!
The difference is that the top.location.href is always index.html, the vars are submitted to the frameset directly.
Here is the code for the pages: <SCRIPT language="JavaScript"><!-- // Test if this window is the toplevel-window, which is not wanted if (top == self) { // the file the user wants to see // I used PHP here, because there is no secure method for this in JS // You could try it with lastIndexOf('/'), but this can get hairy with subdirectories var searchString = '<?echo $REQUEST_URI?>'; // In which frame does the file belong? var menuFile; var mainFile; if (searchString.match('menu.php?')) { menuFile = searchString; window.open('index.html','_blank'); } else { mainFile = searchString; window.open('index.html','_blank'); } } //--></SCRIPT>And this is my frameset: <SCRIPT LANGUAGE="JavaScript"><!-- var mainFrame; var menuFrame; // this is necessary to avoid errormessages when there is no opener if (opener != null) { // determine which frame should be changed if (opener.mainFile != null) { mainFrame = opener.mainFile; // this is not perfect ;( The user needs to confirm. opener.close(); } if (opener.menuFile != null) { menuFrame = opener.menuFile; opener.close(); } } // this is the actual framest document.write('<FRAMESET rows="126,*,22" FRAMESPACING="0" BORDER="0">'); document.write('<frame name="top" src="top.php" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING=no FRAMEBORDER="0" NORESIZE>'); document.write('<FRAMESET cols="170,*" FRAMESPACING=0 BORDER=0>'); // if the var menuFrame is set, the requested URI is loaded, otherwise the default page. document.write('<frame name="menu" src="' + (menuFrame ? unescape(menuFrame) :'menu_blank.html') + '" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING=auto FRAMEBORDER="0" NORESIZE>'); // same with the mainFrame document.write('<FRAME NAME="main" src="' + (mainFrame ? unescape(mainFrame):'news/index.php') + '" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING=yes FRAMEBORDER="0" NORESIZE>'); document.write('<\/FRAMESET>'); document.write('<frame name="bottom" src="bottom.php" MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING=no FRAMEBORDER="0" NORESIZE>'); document.write('<\/FRAMESET>'); //--></SCRIPT>