Re: handling bubling events in frameset in IE

From: Martin Honnen (mahotrash_at_yahoo.de)
Date: 01/22/05


Date: Sat, 22 Jan 2005 14:31:20 +0100


selperin wrote:

> I am looking the way to handle events which occur in frames from the frameset.
> According to DOM documentation - events should bubble up in the object model.

But the DOM is only about a single document in which events bubble up
the tree (in the W3C DOM there are two phases, first down the tree from
the root to the target then bubbling up back the tree from the target to
the root) while you are looking at frames in a frameset document, the
events do occur inside the documents in the frames but they are
propagated to the frameset.
Only Netscape 4 (low and behold) has the ability using
enableExternalCapture to set up a single handler in the frameset document.

> I setup the code below in the top level frameset(JSP page) to trap
> onkeypress event in the underlying frames
> for testing:
>
> <HTML>
> <HEAD>
> <SCRIPT LANGUAGE="JavaScript">
> if (document.layers) {
> window.captureEvents(Event.KEYPRESS);
> window.onkeypress = function (evt) {
> alert(evt.type + ' with key ' + evt.which);
> }
> }
> function keyHandler (evt) {
> alert(evt.type + ' with key ' + (evt.which || evt.charCode ||
> evt.keyCode));
> }
> function initEventHandling() {
> if (!document.all && !document.layers)
> for (var f = 0; f < frames.length; f++)
> frames[f].document.addEventListener('keypress', keyHandler, true);
> }
> </SCRIPT>
> <SCRIPT LANGUAGE="JScript">
> function initEventHandling () {
> for (var f = 0; f < frames.length; f++)
> frames[f].document.attachEvent('onkeypress', ieFrameChecker);
> }
> function ieFrameChecker () {
> for (var f = 0; f < frames.length; f++) {
> try {
> var event = frames[f].event;
> var type = frames[f].event.type;
> event.frame = frames[f];
> keyHandler(event);
> }
> catch (e) {
> // ignore e as we are just looking for the
> // frame which has a valid event object
> }
> }
> }
>
> function keyHandler (evt) {
> alert(evt.type + ' in frame ' + evt.frame.name + ' with key ' +
> evt.keyCode);
> }
>
> </SCRIPT>
> </HEAD>
> <FRAMESET ONLOAD="initEventHandling()" ROWS="50%, 50%">
> <FRAME NAME="frame0" SRC="whatever.html">
> <FRAME NAME="frame1" SRC="whatelse.html">
> </FRAMESET>
> </HTML>

You are trying to have your script itself then propagate events from the
frame documents to the parent document, that should work I think:
<http://home.arcor.de/martin.honnen/javascript/200501/test2005012201.html>
Keypress events are handled in the frameset here for me with Netscape 4,
IE 6, Netscape 7, and Opera 7, all tested on Windows.

Of course for the browsers besides Netscape 4 the handlers are attached
to the the currently loaded documents in the frames meaning if someone
loads a new page in a frame (e.g. by following a link in that frame) the
handlers are gone. With IE 5.5/6 and with Netscape 7 (or other Mozilla
browsers) you could however make use of
   <frame onload
to make sure you set up the event handler every time a new document has
been loaded in that frame.

-- 
	Martin Honnen
	http://JavaScript.FAQTs.com/


Relevant Pages

  • Re: radio button question
    ... I created a KeyDown event handler and it works with one ... Radio buttons can be tricky. ... ..Net frame does) and disable the frame before manipulating the ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: IE6 erratic behaviour
    ... Otherwise do a Google Groups search for "mark of the web" ... > ->When I upload the site to the web: IE6 shows everything as it should, the> result is identical to what I get with Netscape 7.2. ... > In Netscape the result follows the modifications made, IE simply doesn't> want to display any file in the frame. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: IE6 erratic behaviour
    ... >I run XP SP2 with IE 6.0.2900 ... > the result is identical to what I get with Netscape 7.2. ... > want to display any file in the frame. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: SetFocus on a userform Does Not Work
    ... txtClientNumber_Exit handler. ... When the entry is invalid, ... >textboxes inside the frame. ... > Dim StrClientName As String ...
    (microsoft.public.word.vba.general)
  • IE6 erratic behaviour
    ... ->When I upload the site to the web: IE6 shows everything as it should, ... In Netscape the result follows the modifications made, ... want to display any file in the frame. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)