cancelling keydown event in designmode(rich text editing) in firefox

Discussion in 'Javascript' started by pvsundarram@gmail.com, May 2, 2007.

  1. Guest

    hey,
    i am trying to cancel the keydown event for certain keycodes( for eg:-
    enter key ).But the cancelling of this event is not happening in
    firefox. Is there any way to cancel the event in the iframe.
    CODE
    =====
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" lang="en_US"
    xml:lang="en_US">
    <HEAD>
    <SCRIPT>
    var isIE = document.all;
    function chgSpan() {
    var newState;
    var currentState;
    var oSpan = GetObject( "oSpan" );
    var oBtn = GetObject( "oBtn" );
    InitEventListener();
    currentState = (isIE)? oSpan.isContentEditable :
    (oSpan.contentDocument.designMode == "on") ;
    newState = !currentState;
    if(isIE)
    oSpan.contentEditable = newState;
    else
    oSpan.contentDocument.designMode = (newState)? "on" : "off";
    newState==false ? oBtn.value="Enable Editing" :
    oBtn.value="Disable Editing";
    }
    function onEnterEvent(e)
    {
    if(!e)
    e = window.event;
    var key = (isIE)? e.keyCode : e.which;
    alert("onkeydown");
    if(key == 13)
    {
    alert("enter");
    return false;
    }
    }

    function InitEventListener()
    {
    if(isIE)
    {
    window.document.body.attachEvent("onkeydown",onEnterEvent);
    }
    else
    {

    document.getElementById("oSpan").contentWindow.addEventListener("keydown",
    onEnterEvent, false);
    }
    }


    function a(ev){
    var e = (ev!=undefined)?ev :event;
    alert(e);
    }

    function Init()
    {
    var EditorBox = (isIE)?
    document.createElement("div"):document.createElement("iframe");
    EditorBox.id = "oSpan";
    EditorBox.name = "oSpan";
    document.body.appendChild(EditorBox);
    }
    function GetObject(idString)
    {
    return document.getElementById(idString);
    }
    </SCRIPT>
    </HEAD>
    <BODY onload="Init();">
    <P>Click the button to enable or disable SPAN content editing.</P>
    <P>
    <input type="button" id="oBtn" value="Enable Editing"
    onclick="chgSpan();"/>
    </P>
    <P></P>
    </BODY>
    </html>

    Thanks in advance....
    --
    keep klickin',
    P V Sundarram.
    , May 2, 2007
    #1
    1. Advertising

  2. wrote:
    > hey,
    > i am trying to cancel the keydown event for certain keycodes( for eg:-
    > enter key ).But the cancelling of this event is not happening in
    > firefox. Is there any way to cancel the event in the iframe.
    > CODE


    http://developer.mozilla.org/en/docs/Rich-Text_Editing_in_Mozilla#Internet_Explorer_Differences

    [-- A further difference for Mozilla is that once a document is
    switched to designMode, all events on that particular document are
    disabled. Once designMode is turned off however (as this now seems
    possible in Mozilla 1.5) the events become active again. --]



    > var isIE = document.all;


    var isIEOrOpera = document.all
    scripts.contact, May 2, 2007
    #2
    1. Advertising

  3. Guest

    hey,

    On May 2, 9:52 am, "scripts.contact" <>
    wrote:

    > [-- A further difference for Mozilla is that once a document is
    > switched to designMode, all events on that particular document are
    > disabled. Once designMode is turned off however (as this now seems
    > possible in Mozilla 1.5) the events become active again. --]
    >


    i am running firefox(2.0.0.3/Mozilla 5.0), the events are always
    active and they get reported....
    the problem i am facing is, the eventhandler is getting called after
    the character is getting rendered, and in the end i am unable to
    suppress that event.

    is there any other way by which one can suppress the event....
    > var isIEOrOpera = document.all

    Noted.... ;-)

    Thanx again.....

    keep clickin',
    P V Sundarram.
    , May 2, 2007
    #3
  4. rf Guest

    <> wrote in message
    news:...
    > hey,
    > i am trying to cancel the keydown event for certain keycodes( for eg:-
    > enter key ).But the cancelling of this event is not happening in
    > firefox. Is there any way to cancel the event in the iframe.


    e.preventDefault();

    <snip rather irregular code>

    --
    Richard.
    rf, May 2, 2007
    #4
  5. Guest

    hey,
    On May 2, 10:32 am, "rf" <> wrote:
    >
    > e.preventDefault();
    >

    it doesnt work....the eventhandler is getting called after the
    character is getting rendered.....

    Thanx again.....

    keep clickin',
    P V Sundarram.
    , May 2, 2007
    #5
  6. rf Guest

    <> wrote in message
    news:...
    > hey,
    > On May 2, 10:32 am, "rf" <> wrote:
    >>
    >> e.preventDefault();
    >>

    > it doesnt work....the eventhandler is getting called after the
    > character is getting rendered.....


    Hmmm.

    Where did you preventDefault();?

    I am currently writing a an RT editor and it works for me.

    Ok. Timeout. <copy/paste>

    Did you notice that a single enter produces two "enter"s (that is <br>'s)
    in the iframe?

    I removed the alert(onkeydown) and added, in its place, e.preventDefault();

    That changed the rules. No keystrokes at all are passed on to the iframe,
    *except* the enter key. It's being passed to the iframe from somewhere else.
    This should give you something to work on.

    BTW your code is very hard to read/debug. Layout is bad, but that could be
    my newsreader. Some comments might help.

    You are doing some things that you should not do, for example relying on
    document.all to suggest IE and only IE. Check for the feature you are just
    about to use, not some random other one.

    BTW 2, intercepting the enter key and trying to duplicate what browsers do
    (so as to presumably make them all work like IE, which gets it right in this
    instance) is not the way to go for a RT editor. You will end up with lots
    and lots of convoluted DOM manipulations. I know. I have been there. Look at
    fckeditor or tinymce etc for example. Dreadfull code.

    Simply let the browser do whatever it wants to do with the enter key and
    then sneak in afterwards and clean up the mess. WYMeditor takes this
    approach.

    --
    Richard.
    rf, May 2, 2007
    #6
  7. brunascle Guest

    On May 2, 8:59 am, Randy Webb <> wrote:
    > var isIEOrOperaOrAnyOtherBrowserThatSupportsDocumentDotAll = document.all


    var isBrowserThatReturnsTrueForDocumentDotAll = document.all;

    // because some browsers support it but return false, to help with
    browser identification and/or to help choose the right implementation
    to use
    brunascle, May 2, 2007
    #7
  8. Guest

    hey,
    On May 2, 1:20 pm, "rf" <> wrote:


    > Where did you preventDefault();?
    >


    if(key == 13)
    {
    e.preventDefault();
    return false;
    }
    and it works...... but it doesnt work when you put an alert there....

    any idea why it would behave in this fashion????

    >
    > I removed the alert(onkeydown) and added, in its place, e.preventDefault();
    >
    > That changed the rules. No keystrokes at all are passed on to the iframe,
    > *except* the enter key. It's being passed to the iframe from somewhere else.
    > This should give you something to work on.
    >
    > BTW your code is very hard to read/debug. Layout is bad, but that could be
    > my newsreader.

    something to do with ur reader i guess....;-)

    keep klickin',
    P.V. Sundarram
    , May 3, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    442
  2. Erik
    Replies:
    3
    Views:
    541
  3. Martin Honnen

    Rich Text Editing

    Martin Honnen, Jan 2, 2005, in forum: Javascript
    Replies:
    1
    Views:
    86
    Börni
    Jan 2, 2005
  4. Replies:
    2
    Views:
    165
  5. deostroll
    Replies:
    1
    Views:
    151
    deostroll
    Jan 11, 2008
Loading...

Share This Page