- Create Account

addEvent cross frames doesn't work... but i have a fix

Forums > Cross-Browser DHTML API > addEvent cross frames doesn't work... but i have a fix
Author
Message

1) puckpuck Group: Members
Posts: 1 Joined: 23 Dec 2005 Location: Windsor, ON, Canada IP: 72.38.--.--
Essentially the event object doesn't come through. So I made a real small fix for it, by adding a reference to the parent object (frame) and have the event object come through properly.

The problem occurs for me under IE 6 on win xp sp2. The main code is included in the parent frameset page. From the framed page itself, when it loads I leverage the parent page's function reference by doing this

parent.setupObjects();

In the parent page I would include the setupObjects function which creates a new layer on the framed page. It will also add event handlers to this layer.

When the event is fired my event object would be null, but I was able to do parent.frames.contentFrame.event to get a value. I didn't like having to hard code the parent object in my event handler just to reference the event. So my solution was to add a reference to the parent object through the addEvent function. I only did this to the non NS4 version, as I haven't had an NS4 browser hit my site in the past year.

New addEvent function with a reference to the parent object (p). The other addEvent function (NS4 version) must be commented out or you'll conflict due to identical function signatures (or add a parent reference to the NS4 version also).

function addEvent(o, p, n, f)
{
 var a='addEventListener', h='on'+n;
 if (o[a]) return o[a](n, f, false);
 if (o[h])
 {
o._c |= 0;
  var  b = '_b' + (++o._c);
  o[b] = o[h];
}
 o[h] = function(e)
 {
p=p?p:self;
  e=e||p.event;
  var  r = true;
  if (o[b])  r = o[b](e) != false && r;
  o._f=f;
  r = o._f(e)  != false && r;
  return r;
 }
};

2) Angus Turnbull Group: Moderators
Posts: 4042 Joined: 7 Dec 2003 Location: New Zealand IP: 210.54.--.--
Good modification :). Yeah, by default addEvent() is designed more to be included in each frame where it's used, but that approach is a perfectly valid tweak.

- Angus.

Post a Reply:

You are not logged in, and will be posting anonymously as a guest. If you want to post using an account, please login at the top of this page.