Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using event handlers as javascript properties and referencing frames

Status
Not open for further replies.

theprofessorx

Programmer
Mar 21, 2002
10
0
0
US
i have a frameset with a top navigation frame (topFr) and a nested frameset with a left navigation frame (leftFr) and a content frame (contentFr). i am trying to code a handler for the event "onunload" triggered by contentFr and i don't want to use the HTML attribute as the handler.

i've placed the following code within script tags in the head section of the frameset page to test that contentFr object is referenced correctly: window.contentFr.onunload = function() {alert('you are unloading this page');}. no alert box comes up.

however, if i use the same code for the top level window such as: window.onunload = function() {alert('you are unloading this page');} - the alert box appears.

can somebody let me know why this alert does not appear when referencing contentFr and how to fix it? thanks much in advance.
 
It sounds like you want to have the function occur when the document in the frame unloads, not the window. Try using the following:

document.contentFr.onunload = function();

 
document.contentFr.onunload = function(); will not work because it violates the object model hierarchy. Documents reside within Windows. and Frames are technically Windows. contentFr is a Frame.

it's interesting because the onLoad event is recognized within a Frame tag (ie - <FRAME name=&quot;contentFr&quot; onLoad=&quot;function();&quot;) but the onUnload event is not recognized in a Frame tag. yet both are allowed in the Frameset tag. however, when using the onload as a property (ie - top.contentFr.onload = function();), nothing happens. i'm still not sure if the frame object isn't being referenced correctly, or if this piece of javascript code just plain won't work. any thoughts?
 
There are several ways to access a child frame:
Code:
document.frames.myChild
document.myChild
window.frames.myChild
window.myChild
frames.myChild
myChild
As far as I know, you referenced it fine. There doesn't seem to be any reason why your code doesn't work ... Other than
1. The method by which the user leaves the page doesn't trigger the
Code:
onunload
event when it is applied like this
2. You're not allowed to apply an
Code:
onunload
event to a different page
3.
Code:
contentFr
must be recognized before applying an
Code:
onunload
event handler to it

My only suggestion would be to try putting the script after the <frameset></frameset> to test whether or not the third case is true. If not, I would suggest trying to put the event handler in the <body> tag of the frame which you're trying to detect it for.
Code:
- UNIMENT
 
i'm using IE5. in response to your responses ...
1. we know the event is triggered because it works at the top level window (ie - window.contentFr.onload = function();)
2. i'm not exactly sure what you mean. but the unload event is being applied to the window that holds the page
3. the contentFr isn't actually being manipulated before rendered in the browser, so i would think its ok to define the handler in the head.

in response to your suggestions ... i tried placing the script after the </FRAMESET> tag. it doesn't work. i don't want to put the event handler in the <BODY> tag because that would be a maintenance nightmare. i would have to do that for every document that gets displayed in the contentFr.

documentation says the onload handler is supposed to be applied to the <BODY> or <FRAMESET> tags only and can also be used like window.load (as a property of the Window object). i guess the event handler just doesn't work for child frames as a property.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top