tags:

views:

59

answers:

1

I have a frame page....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" 
"http://www.w3.org/TR/html4/frameset.dtd"&gt;

<html>
  <head>
     <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
     <title>Main title</title>
    <script type="text/javascript" src="JS.js"></script>
  </head>
  <frameset rows="10%,90%" id="frameid" name="frameName">
     <frame src="FrameA.htm"     name="F_A">
     <frame src="FrameB.htm"     name="F_B">
  </frameset>
</html>

Here's FrameA.htm; FrameB looks similar

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
   <head>
     <meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
     <script type="text/javascript" src="JS.js"></script>
     <title>Frame A Title</title>
   </head>

   <body onload="parent.ldfunc()" class="Resize" id="idA">
      Body of frame A 
  </body>
</html>

and finally a javascript JS.js

function ldfunc () {
  alert("window.self.name: " + window.self.name);
  alert("window.self.document.body.id: " + window.self.document.body.id);
}

Now here's the rub...

1) If the onload clause in the body tag is parent.ldfunc() as it is above, 'self' seems to be the frame page, not page A or page B. I.e. Id is displayed as frameid.

2) If the onload clause is ldfunc() WITHOUT the parent, 'self' points at page A or page B as expected. I.e. id is displayed as idA or iadB.

In other words, self seems to follow the function location, not the page.

Why did I try this? I'm going to have several thousand pages that will be loaded in B by A and I thought I could eliminate the need to put the script in each of their headers.

Is there some way to make the script loaded by the frame page global and available to the other pages?

+2  A: 

window inside that function is not dynamically referenced. The reference to window is created on the moment the function is declared and is thus always the window reference to where the function resides. This is called a closure, when you basically embed external references inside a function at declare time.

You need to pass the context window as an argument upon calling the function.

Put scripts inside the top frame/window. Call functions with:

top.funcName();

And pass context.

top.funcName(window);
BGerrissen
+1. And to clarify, all `self` is is a property of `window` pointing back to the same `window`. It's of no use to anyone ever.
bobince
Does the frameset page NOT have a window? Do only pages with a body have a window?
Mike D
the frameset page is the top frame/window and does have a window object.
BGerrissen
Does top have a document? How would I go about referencing the name and id on the frameset? Does top have different meanings depending on context? For example it looks like a window when I reference top.frames.length but what is it when I reference top.F_A.document.whatever?
Mike D
top or window.top is simply a reference to the top most window (in this case the html page holding the frameset. So yeah, it has a document referable by top.document. As for refering to a child frame from another child frame, don't. Let all your frames introduce themselves to the top level code and let the top level code introduce them to eachother. Avoid direct access, it will only make your code more complicated.
BGerrissen
@BGerrissen: Could you expound a bit on what you mean by "Let all your frames introduce themselves to the top level code and let the top level code introduce them to each other." What I've done is have all the JS in the top page. It refers to things in the lower pages and is really the only thing cognizant of the structure. I wonder if you have something else in mind. I could imagine how one page could generate events and report them to the top but I don't see a way for other pages to react to these events.
Mike D
I misread, thought you had code in your frames trying to reach other frames, keeping all code in the top window (frameset page) is a good strategy. If you did have code in the frames that needed to reach other frames and vice versa, then setting up a publisher object in the top frame allowing scripts from frames to listen to and fire events on would be a good way of decoupled messaging.
BGerrissen