Forum Moderators: open

Message Too Old, No Replies

onLoad: Can it change a separate frame?

         

Msquared

9:10 pm on Jun 14, 2002 (gmt 0)



New to javascript and working on a site with frames. I have successfully changed two frames with one click using onclick="window.self.location=###.htm'". But as previously noted if the user clicks back button, they are only taken back one frame. I am trying to use this option because my 'client' wants a frames page, but they also want a horizontal navigation bar with graphics that change to alert you to what page you are on. Ok, after all this rambling, what I really want to know is is there an onLoad script that will change the left frame when the main frame page is loaded (after clicking of course) to work around this back button issue? If not, are there any other fixes?
And if there are no fixes, will the changing two frames with one click work with an image mapped graphic?

jatar_k

9:31 pm on Jun 14, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WmW Msquared

I am reading up on how to do this in my trusty o'reilly's book and it seems to say that both IE and NN treat frames similar to window objects. Therefore you can change their url or src by targeting it using its name.

maybe something like this?

onLoad="changeSideFrameUrl();"

function changeSideFrameUrl () {
sideframe.location = "somepage.html";
}

it says urls can be full or relative and it would seem that this should work but I am far from a js expert.

rewboss

5:54 am on Jun 16, 2002 (gmt 0)

10+ Year Member



I've found using JavaScript to access other frames to be unreliable at best; I suggest you make sure you test thoroughly on as many browser/platforms as you can.

The first thing to understand is how frames are referenced. Suppose you have this frameset:

<frameset cols="200,*">
<frame name="left" src="nav.html" />
<frame name="main" src="index2.html" />
</frameset>

The main top-level browser window (which has no name) now has two "children", called "left" and "main". These children are stored as properties of the top-level window itself.

You have a script in main which wants to change properties in left. You can't access left directly, you have to access it as a property of the parent window:

window.parent.left

The location object of left can be accessed like this:

window.parent.left.location

What you want to do (if possible) is to load a different document into "left" without creating a new entry in that window's History list. There is a method that will do that: it's Location.replace(), and you use it like this:

window.parent.left.location.replace('newpage.html');

I can't guarantee this will work exactly as you want it to (it's worth a try, though). Another possibility is this: if it's only a graphic that you want to change, and if you make all of the "This Is The Page You're On" graphics the same width and height, you can simply change the graphic in the same way you do for JavaScript rollovers. If you have this <img> tag in the left frame:

<img src="home.gif" name="heading" />

...you can change it from the main frame like this:

window.parent.left.heading.src="newpage.gif";

However, I would strongly recommend talking your client out of using frames at all. Here are the reasons:

1. Search engines have trouble with them.
2. They are passé.
3. Future browsers might not support them.
4. They increase the load on the server, making it slower, less efficient and also raising costs.

(OK, that's a little contrived. The problems involved with using frames are more subtle than that, but you have to hit clients where it hurts: tell them it's expensive and won't get them indexed on search engines and they'll come around to your way of thinking sooner or later.)

Msquared

9:33 pm on Jun 17, 2002 (gmt 0)



So, I hate to sound like such a newbie . . . but does the 'window.parent.left.nowhome.src="###.jpg"' go in the body tag?

jatar_k

10:23 pm on Jun 17, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Msquared,

I would put a function in the onLoad as you said and then the rollover or replace(); method would go in the actual function in the head portion of the page.

Msquared

10:46 pm on Jun 17, 2002 (gmt 0)



Thanks!

Reno

4:28 am on Jun 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rewboss I want to thank you for that most excellent explanation. Well written and clearly presented - I learned a lot.

Purple Martin

5:08 am on Jun 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good reply rewboss :) You mention that using JavaScript can be unreliable and you mention that understanding referencing is important. I'd like to try to add a little general tip about that:

I've found that when I need to write cross-browser JavaScript that references page element objects I save myself a lot of hassle by always specifying both id and name attributes. That way it doesn't matter which browser-specific code is being used (document.all vs document.layers vs document.getElementById). For example:

<frameset cols="200,*">
<frame id="left" name="left" src="nav.html" />
<frame id="main" name="main" src="index2.html" />
</frameset>

Msquared

8:40 pm on Jun 18, 2002 (gmt 0)



Hi, me again. I'm trying desperately to teach myself this javascript stuff, but I'm stuck.
I'm vacationing in the Bahamas and got coerced to doing this for a friend because I just happened to take one html class and one FrontPage class last year. That does not mean I know what I'm doing :) and I have no books with me. No reference material other than what I've found on the web and you guys. And I do appreciate your help so much. Soooooo . . . this is what I've got, but it doesn't work. Is there something wrong with the syntax? Any suggestions? Other than get rid of the frames all together? Believe me I'm pleading with my friend.

<html>

<head>
<script type = "javascript">
<!--
function changeImage()
{
window.parent.left.nowhome.src="home_now.jpg";
//-->
</script>

<title>Our Services</title>
<base target="rbottom">
</head>

<body topmargin="0" bgcolor="#FFFFFF" onLoad="changeImage()">

jatar_k

8:48 pm on Jun 18, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about a sticky with a url in it?

<added>some errors

<html>
<head>
<title>Our Services</title>
<base target="rbottom">

<script lang="javascript">
<!--
function changeImage()
{
window.parent.left.nowhome.src="home_now.jpg";
}
//-->
</script>

</head>

<body topmargin="0" bgcolor="#FFFFFF" onLoad="changeImage();">

you didn't close the brace at the end of the function

Msquared

11:33 pm on Jun 18, 2002 (gmt 0)



I've got it working!!! Thank all of you for all of your help!