PDA

View Full Version : How to redirect into a frame....


aceinstaller
01-16-2001, 12:54 PM
Hi. Can someone please suggest a piece of Javascript code to redirect a page into a specific frame. Any help you can offer is much appreciated.

Andy

------------------
http://www.ace-installer.com
webmaster@ace-installer.com

kdjoergensen
01-16-2001, 04:07 PM
Frameset:

<html>
<script language="javascript">
<!--
var s = unescape(window.location.search.substring(1));
var url = (s.length > 2) ? s : "homepage.html";

function loadFrames(){
top.frame1.location = "nav.html";
top.frame2.location = url;
}
//-->
</script>

<frameset onload="loadFrames()">
<frame name="frame1" src="" noscrolling>
<frame name="frame2" src="" border=0>
</frameset>
</html>

in individual pages:
<script language="javascript">
if (top.location == self.location) {
window.location = "index.html?"+escape(self.location);
}
</script>

Q & A:
all src properties in the frameset are empty. why ? to ensure onload event handler in frameset fires fast and load frames.

can I only use 2 frames ? NO, if you want more frames simply add them to the script (e.g. top.frame45.location = "frame45doc.html").

why can I not just load frame2 as follows: src="javascript: loadFrame()" ??
beats me.. it works in NS4 and IE4/5, but not in NS6 (*sigh*). Above SHOULD work in all browsers mentioned.

what is all this with: if (s.length >2) ??
I could have written: if (s), but I am being overly cautious here.. (dont ask).

what is the code in the individual pages ?
- it redirects to your frameset document. it passes with it a parameter (the url of the page itself) in the search property of the location object. This information is what the frameset document is extracting on the first lines of code and it is needed for the browser to know which page to load into your page. If no parameter passed along (if someone entered your index.html directly) then the default homepage get loaded.

Do I have to keep the names hompage.html, nav.html and index.html ? NO, just change the names in the script section, too.

How can I cut down on updates ?
You can cut down on updates in your pages if you link in the individual page code in an internal javascript file, e.g.:
<!--
if (top.location == self.location) {
window.location = "index.html?"+escape(self.location);
//-->
Copy and paste everything between and including <!-- and //--> into an empty word processor document and save as text with extention .js
(javascript external file). EXAMPLE: frame.js
To link it to the individual pages:
<SCRIPT LANGUAGE="JAVASCRIPT" SRC="FRAME.JS"></SCRIPT>
(goes in the individual pages)

Why would you need to do that ? why not include the code on all pages ?
- if you change web host, for example, and want to change the url of the page being loaded you need only to change ONE place (the js file) instead of on every page... smart eh ??




[This message has been edited by kdjoergensen (edited 01-16-2001).]

Jacob
01-16-2001, 04:53 PM
I use the following for redirecting pages in certain frames:

<BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre>
&lt;script language="JavaScript1.2"&gt;
&lt;!--
function jump()
{
parent.mainframe.location = 'http://www.file.com/file.html'
}
//--&gt;
&lt;/script&gt;
[/code]

mainframe = name of the frame I want the file to display in.

You can execute the javascript in a couple ways. Using a link:

<BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre>
&lt;a href="javascript:jump()"&gt;Click here&lt;/a&gt;
[/code]

or in your body tag:

<BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre>
&lt;body onload="jump()"&gt;
[/code]

Regards,
[/code]

------------------
Jacob A. Wheeler
Co-Founder / Web Engineer
Big Resources Network
jacob@bigresources.com
ICQ: 390147 (http://www.icq.com/390147)

aceinstaller
01-17-2001, 03:43 AM
Thanks to both of you for your help! I have now managed to do it via Jacob's way (cos it was simpler!!!), thanks to kdjoergensen though for your help.

Andy

------------------
http://www.ace-installer.com
webmaster@ace-installer.com

kdjoergensen
01-17-2001, 01:47 PM
Sorry ace, I misunderstood your question.