captureEvents in Netscape 7

  • Thread starter Vincent van Beveren
  • Start date
V

Vincent van Beveren

Hey everyone,

I have trouble capturing events in Netscape 7.1.

I am building a WYSIWYG editor thingy which should both work in IE and
NS 7. For this I use designMode='on'. However, it seems like as soon as
I do that, it stops capturing events from that element.

//
// EditGUI is a simple WYSIWYG editor for chat
///////////////////////////////////////////////////////////////

function EditGUI() {
this.document = null;
this.window = null;
this.body = null;
this.content = null;

this.writeHere = function(width, height, className)
{
iframe = writeIFrame(width,height, className);
this.window = iframe.contentWindow;
this.document = this.window.document;
this.body = this.document.body;


// make it editable
if ("contentEditable" in this.body) {
// IE
this.content = this.document.createElement('P');
this.content.contentEditable = true;
this.body.appendChild(this.content);
} else if ("designMode" in this.document) {
// NS 7
this.content = this.document.body;
this.document.designMode = "on";
}

// install callback reference
this.document.backRef = this;

if ("captureEvents" in this.document) {
this.document.captureEvents(Event.KEYPRESS);
}

this.document.onkeypress = function(event) {
event = (event!=null?event:window.event);
this.backRef.key(event);
}


}

this.key = function(event) {
alert('yes, you pressed a key!');
}
}

//
// appends an iframe to the current part of the document and
// returns its reference.
/////////////////////////////////////////////////////////////////

function writeIFrame(width, height, className) {
iframe = document.createElement('IFRAME');
if (className) iframe.className = className;
iframe.style.width=width;
iframe.style.height=height;
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
doc.open();
doc.close();
return iframe;
}

Now this works perfectly in Internet Explorer. But whatever I try, I
can't get Netscape to display 'yes, you pressed a key'. There are no
errors either. It does work if I turn designMode off. Anyone have any idea?

Thanks in advance,
Vincent
 
M

Martin Honnen

Vincent van Beveren wrote:

[captureEvents]
I have trouble capturing events in Netscape 7.1.

The method captureEvents is part of the Netscape 4 event model/api, it
also somehow made it into Mozilla and Netscape 6/7 but shouldn't be used
there as they implement the W3C DOM Level 2 event model/api which has
its own way to capture events i.e.
document.addEventListener(
'keypress',
function (evt) {
alert(evt.type);
},
true
);
Looking at your code I don't think you really want to use event
capturing in the sense of the DOM Level 2 event model but simply want to
handle the keypress event at the document node so you can probably use
false instead of true above.
I haven't tested whether handling key events with Netscape 7 works
properly in an iframe with designMode editing enabled but if it doesn't
that is most certainly not a problem cured with captureEvents in any
way. You might want to search bugzilla.mozilla.org whether there are any
bugs filed for key event handling in editable iframes maybe they have
been fixed after Netscape 7.1 (which is based on Mozilla 1.4).
 
V

Vincent van Beveren

Thanks for your quick reply.
The method captureEvents is part of the Netscape 4 event model/api, it
also somehow made it into Mozilla and Netscape 6/7 but shouldn't be used
there as they implement the W3C DOM Level 2 event model/api which has
its own way to capture events i.e.

The captureEvents was a last resort. I also included it so people
wouldn't reply with 'did you try to use captureEvents'.

I tried your code, but then it doesn't work at all, not even in IE.

this.document.addEventListener('onkeypress',
function(event) {
event = (event!=null?event:window.event);
this.backRef.key(event);
}, true); // also tried false


Neither does it give any error... hmm...

You might want to search bugzilla.mozilla.org whether there are any
bugs filed for key event handling in editable iframes maybe they have
been fixed after Netscape 7.1 (which is based on Mozilla 1.4).

That could be. But even if it wouldn't solve it. It needs to run for an
as broad audience as posssible.

any other ideas?

Vincent
 
M

Martin Honnen

Vincent van Beveren wrote:

The captureEvents was a last resort. I also included it so people
wouldn't reply with 'did you try to use captureEvents'.

I tried your code, but then it doesn't work at all, not even in IE.

Well, I have not suggested to use addEventListener for IE.
addEventListener is part of the W3C DOM Level 2 events model and IE
doesn't implement that.
 
M

Martin Honnen

Vincent van Beveren wrote:

I tried your code, but then it doesn't work at all, not even in IE.

this.document.addEventListener('onkeypress',
function(event) {
event = (event!=null?event:window.event);
this.backRef.key(event);
}, true); // also tried false

I have made a short example at
http://home.arcor.de/martin.honnen/javascript/200405/test20040528.html
and here with Netscape 7.1, some Mozilla 1.7a and Firefox 0.8 all on
Windows the keypress event listener is called when I type in the iframe.
 
T

Thomas 'PointedEars' Lahn

Vincent van Beveren wrote:

Please provide proper attribution. A line including the
author of the quoted material is sufficient (see above).
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
The captureEvents was a last resort. I also included it so people
wouldn't reply with 'did you try to use captureEvents'.

I tried your code, but then it doesn't work at all, not even in IE.

this.document.addEventListener('onkeypress',

The event identifier is "keypress", its intrinsic handler is
"onkeypress". addEventListener() expects the identifier, not
the handler.
function(event) {
event = (event!=null?event:window.event);
this.backRef.key(event);

Looks like fantasy syntax.
}, true); // also tried false


Neither does it give any error... hmm...

1. Why should it? There could be an "onkeypress" event in a DOM.
2. Maybe it does. Watch Mozilla's JavaScript console.


PointedEars
 
V

Vincent van Beveren

Thanks everyone for replying!
this.document.addEventListener('onkeypress',
function(event) {
event = (event!=null?event:window.event);
this.backRef.key(event);
}, true); // also tried false

The problem was that it should have been 'keypress' and not
'onkeypress'. Now it works perfectly, though I had to make
an exception for IE. I wrote the following code:

this.document.backRef = this;
this.document.onkeyup = function(event)
{
event = (event?event:this.parentWindow.event);
this.backRef.keyUp(new WrappedEvent(event));
};

if (this.document.addEventListener) {
this.document.addEventListener('keyup', this.document.onkeyup,
false);
}

I choose key up later, cause it suited the cause better.
The wrappedEvent object is a generic event object so that I
don't need to worry about incompatibility issues. (blah)

I wish that IE was somewhat more DOM complient.

Thanks,
Vincent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top