IE Not Stopping Default Event

Discussion in 'Javascript' started by blaine@worldweb.com, Apr 5, 2007.

  1. Guest

    Hello,

    I'm currently overriding function keys (F1 to F4) to perform other
    actions. In order to do this the default popup windows of Help (F1),
    Seach(F3) etc must be turned off. In FF it's easy enought to do using
    the preventDefault and stopPropagation event functions.

    IE's equivalent is supposed to be cancelBubble and returnValue,
    however I can not seem to get them to stop no matter what I try.

    Can someone please point out my error? The test code is below.

    ------
    var classKeyPressOverride = function(){}
    classKeyPressOverride.prototype.toString = function(){ return " Class
    Key Press Override "; }
    classKeyPressOverride.prototype.attachListener = function( type,
    functionCode ){
    //Register the listener
    var obj = window.document;
    if ('addEventListener' in obj) {
    obj.addEventListener( type, functionCode, true);
    } else if ('attachEvent' in obj) {
    obj.attachEvent('on' + type, functionCode );
    } else {
    alert("Could not add listener");
    }
    }


    //Create an instance and add attach a lister tpe of keydown
    var classKP = new classKeyPressOverride();

    classKP.attachListener('keydown', function(event){
    var ev = event || window.event;

    var keyCode = ev.keyCode || ev.which;
    alert("aatrying " + keyCode);
    var fKeyPressed = false;

    //IE won't like the regular ev.DOM_VK_F1 so use keyCode values.
    if (keyCode == 112) {
    fKeyPressed = true;
    alert("ev F1 Pressed");
    }else if (keyCode == 113) {
    fKeyPressed = true;
    alert("F2 Pressed");
    }else if (keyCode == 114) {
    fKeyPressed = true;
    alert("F3 Pressed");
    }else if (keyCode == 115) {
    fKeyPressed = true;
    alert("F4 Pressed");
    }

    if (fKeyPressed){
    //Prevent help menu and other default F1 key functions
    if ('stopPropagation' in ev) {
    ev.stopPropagation();
    ev.preventDefault();
    }else{
    // Trying to stop the popup windows in IE
    //Does not seem to work...
    ev.cancelBubble = true;
    ev.returnValue = false;
    }
    }

    }
    );
     
    , Apr 5, 2007
    #1
    1. Advertising

  2. Guest

    Hey Gérard,

    Thanks for your concern.

    If you would have thought about it a little longer you may have
    realized that this if for a web application that the general public
    does not have access to.

    Since users a trained to press the K-Keys for help. It makes perfect
    sense to override the Help menu of the browser with the Help Menu of
    the application.

    Sincerely,
    Blaine



    On Apr 5, 3:45 pm, Gérard Talbot <> wrote:
    > wrote :
    >
    > > Hello,

    >
    > > I'm currently overriding function keys (F1 to F4) to perform other
    > > actions.

    >
    > Wow! How nice of you to do this for/to your users.
    >
    > > In order to do this the default popup windows of Help (F1),
    > > Seach(F3) etc must be turned off.

    >
    > Wow! The end must justify the means.
    >
    > > In FF it's easy enought to do using
    > > the preventDefault and stopPropagation event functions.

    >
    > Maybe your code have succeeded in defeating the general purpose of a
    > sound user-author relationship where the webpage should not alienate the
    > user. There is such a thing as "do not alienate your users even though
    > you can/could" guideline, "do not over-power, over-control the users'
    > control over basic functions, keys," etc.
    >
    > What's next? Can you remotedly over-ride the On/Off switch button on the
    > PC of users?
    >
    > > IE's equivalent is supposed to be cancelBubble and returnValue,

    >
    > Overriding the F1 key is like overriding the 911 phone number and what
    > it means to a wide majority of normal, reasonable people. Your code may
    > have succeeded in a global self-defeating strategy.
    >
    > Gérard
    > --
    > Using Web Standards in your Web Pages (Updated Dec. 2006)http://developer..mozilla.org/en/docs/Using_Web_Standards_in_your_Web_...
     
    , Apr 6, 2007
    #2
    1. Advertising

  3. Lee Guest

    said:
    >
    >Hey G=E9rard,
    >
    >Thanks for your concern.
    >
    >If you would have thought about it a little longer you may have
    >realized that this if for a web application that the general public
    >does not have access to.


    What clues did you provide that might have helped anyone to
    realize that? The vast majority of posts in this newsgroup
    are about internet sites, putting the onus on you few others
    to identify your unusual situation.

    It doesn't make sense to over-ride browser functionality.
    Even if your current users are used to some legacy system,
    it makes much more sense to have them adapt to web browser
    standards than to try to pervert the browser to fit them.


    --
     
    Lee, Apr 6, 2007
    #3
  4. Guest

    I just wanted to post the solution for anyone that is interested.

    1) If you want to override the 'F1' button in IE you need to attach an
    event listener to stop
    the default action.
    classKP.attachListener('help', classKP.stopPropagation );

    2) In IE cancelBubble and returnValue were not stopping the default
    actions as specified in the docs, setting the keyCode to 0 then
    running the actions stops all the defaults
    ev.keyCode = 0;


    Below is a working example.


    var classKeyPressOverride = function(){}
    classKeyPressOverride.prototype.toString = function(){ return " Class
    Key Press Override "; }
    classKeyPressOverride.prototype.attachListener = function( type,
    functionCode ){
    //Register the listener

    var obj = window.document;
    obj.eventParent = this;
    if ('addEventListener' in obj) {
    obj.addEventListener( type, functionCode, true);

    } else if ('attachEvent' in obj) {
    obj.attachEvent('on' + type, functionCode );
    } else {
    alert("Could not add listener");
    }
    }
    classKeyPressOverride.prototype.stopPropagation = function(ev){
    if ('stopPropagation' in ev) {
    ev.stopPropagation();
    ev.preventDefault();
    }else{
    ev.keyCode = 0;
    ev.cancelBubble = true;
    ev.returnValue = false;
    }
    }

    //Create an instance and add attach a lister tpe of keydown
    var classKP = new classKeyPressOverride();
    classKP.attachListener('help', classKP.stopPropagation );
    classKP.attachListener('keydown', function(event,parentClass){
    var ev = event || window.event;

    var keyCode = ev.keyCode || ev.which;
    var fKeyPressed = false;

    //ev.DOM_VK_F1
    if (keyCode == 112) {
    fKeyPressed = true;
    //alert("ev F1 Pressed");
    }else if (keyCode == 113) {
    fKeyPressed = true;
    //alert("F2 Pressed");
    }else if (keyCode == 114) {
    fKeyPressed = true;
    //alert("F3 Pressed");
    }else if (keyCode == 115) {
    fKeyPressed = true;
    //alert("F4 Pressed");
    }

    if (fKeyPressed == true){
    this.eventParent.stopPropagation(event);
    }

    }
    );




    On Apr 7, 10:35 pm, Gérard Talbot <> wrote:
    > wrote :
    >
    > > Hey Gérard,

    >
    > > Thanks for your concern.

    >
    > Thank you for not top-posting in comp.lang.javascript newsgroup.
    >
    > > If you would have thought about it a little longer you may have
    > > realized that this if for a web application that the general public
    > > does not have access to.

    >
    > Are you actually saying that you did not think of including such vital
    > info in your original post or are you saying that you assumed that
    > readers of your post would have been able (or should be able) to read
    > your mind over the internet, figure out your intentions and missing
    > information and then establish that your intentions are sound, fair,
    > legitimate, level-headed and that your application requirements are all
    > balanced, sensible and above any reproach?
    >
    > > Since users a trained to press the K-Keys for help.

    >
    > I read your post several times and still don't understand how or why the
    > F1 key should be replaced by the "K-Keys" for help or why it is a good
    > idea to replace the F1 key in an intranet application with the "K-keys".
    >
    > Maybe one day there will be an original urbanist who will redesign color
    > for street lights and he will use purple or gray for stop instead of red
    > and he will choose yellow for going across street intersection instead
    > of green and such colors will only apply to his town, not outside such town.
    >
    > > It makes perfect
    > > sense to override the Help menu of the browser with the Help Menu of
    > > the application.

    >
    > Your words up there. Not mine.
    >
    > > Sincerely,
    > > Blaine

    >
    > Fair enough. Good luck.
    >
    > Gérard
    > --
    > Using Web Standards in your Web Pages (Updated Dec. 2006)http://developer..mozilla.org/en/docs/Using_Web_Standards_in_your_Web_...
     
    , Apr 9, 2007
    #4
  5. Guest

    Ups I problem with the code above..

    The line
    this.eventParent.stopPropagation(event)
    should be:
    classKP.stopPropagation(event);



    var classKeyPressOverride = function(){}
    classKeyPressOverride.prototype.toString = function(){ return " Class
    Key Press Override "; }
    classKeyPressOverride.prototype.attachListener = function( type,
    functionCode ){
    //Register the listener

    var obj = window.document;

    if ('addEventListener' in obj) {
    obj.addEventListener( type, functionCode, true);

    } else if ('attachEvent' in obj) {
    obj.attachEvent('on' + type, functionCode );
    } else {
    alert("Could not add listener");
    }
    }

    classKeyPressOverride.prototype.addListener = function(obj, evt, fun,
    b) {
    var eventPhase = (typeof(b)=='boolean') ? b : false;

    if ('addEventListener' in obj) {
    obj.addEventListener(evt, fun, eventPhase);
    } else if ('attachEvent' in obj) {
    obj.attachEvent(evt, fun);
    } else {
    alert("Could not add listener");
    }
    }

    classKeyPressOverride.prototype.stopPropagation = function(ev){
    if ('stopPropagation' in ev) {
    ev.stopPropagation();
    ev.preventDefault();
    }else{
    ev.keyCode = 0;
    ev.cancelBubble = true;
    ev.returnValue = false;
    }
    }

    //Create an instance and add attach a lister tpe of keydown
    var classKP = new classKeyPressOverride();
    classKP.attachListener('help', classKP.stopPropagation );
    classKP.attachListener('keydown', function(event){
    var ev = event || window.event;

    var keyCode = ev.keyCode || ev.which;
    var fKeyPressed = false;

    //ev.DOM_VK_F1
    if (keyCode == 112) {
    fKeyPressed = true;
    //alert("ev F1 Pressed");
    }else if (keyCode == 113) {
    fKeyPressed = true;
    //alert("F2 Pressed");
    }else if (keyCode == 114) {
    fKeyPressed = true;
    //alert("F3 Pressed");
    }else if (keyCode == 115) {
    fKeyPressed = true;
    //alert("F4 Pressed");
    }

    if (fKeyPressed == true){
    //new classKeyPressOverride().stopPropagation(event);

    //Use the variable name of the class instance
    //Since it is within the scopt of this function code
    // "this" and "parent" refer to the object that the key lister is
    run on ie window.document
    classKP.stopPropagation(event);
    }
    }
    );




    On Apr 9, 11:04 am, "" <> wrote:
    > I just wanted to post the solution for anyone that is interested.
    >
    > 1) If you want to override the 'F1' button in IE you need to attach an
    > event listener to stop
    > the default action.
    > classKP.attachListener('help', classKP.stopPropagation );
    >
    > 2) In IE cancelBubble and returnValue were not stopping the default
    > actions as specified in the docs, setting the keyCode to 0 then
    > running the actions stops all the defaults
    > ev.keyCode = 0;
    >
    > Below is a working example.
    >
    > var classKeyPressOverride = function(){}
    > classKeyPressOverride.prototype.toString = function(){ return " Class
    > Key Press Override "; }
    > classKeyPressOverride.prototype.attachListener = function( type,
    > functionCode ){
    > //Register the listener
    >
    > var obj = window.document;
    > obj.eventParent = this;
    > if ('addEventListener' in obj) {
    > obj.addEventListener( type, functionCode, true);
    >
    > } else if ('attachEvent' in obj) {
    > obj.attachEvent('on' + type, functionCode );
    > } else {
    > alert("Could not add listener");
    > }}
    >
    > classKeyPressOverride.prototype.stopPropagation = function(ev){
    > if ('stopPropagation' in ev) {
    > ev.stopPropagation();
    > ev.preventDefault();
    > }else{
    > ev.keyCode = 0;
    > ev.cancelBubble = true;
    > ev.returnValue = false;
    > }
    >
    > }
    >
    > //Create an instance and add attach a lister tpe of keydown
    > var classKP = new classKeyPressOverride();
    > classKP.attachListener('help', classKP.stopPropagation );
    > classKP.attachListener('keydown', function(event,parentClass){
    > var ev = event || window.event;
    >
    > var keyCode = ev.keyCode || ev.which;
    > var fKeyPressed = false;
    >
    > //ev.DOM_VK_F1
    > if (keyCode == 112) {
    > fKeyPressed = true;
    > //alert("ev F1 Pressed");
    > }else if (keyCode == 113) {
    > fKeyPressed = true;
    > //alert("F2 Pressed");
    > }else if (keyCode == 114) {
    > fKeyPressed = true;
    > //alert("F3 Pressed");
    > }else if (keyCode == 115) {
    > fKeyPressed = true;
    > //alert("F4 Pressed");
    > }
    >
    > if (fKeyPressed == true){
    > this.eventParent.stopPropagation(event);
    > }
    >
    > }
    >
    > );
    >
    > On Apr 7, 10:35 pm, Gérard Talbot <> wrote:
    >
    > > wrote :

    >
    > > > Hey Gérard,

    >
    > > > Thanks for your concern.

    >
    > > Thank you for not top-posting in comp.lang.javascript newsgroup.

    >
    > > > If you would have thought about it a little longer you may have
    > > > realized that this if for a web application that the general public
    > > > does not have access to.

    >
    > > Are you actually saying that you did not think of including such vital
    > > info in your original post or are you saying that you assumed that
    > > readers of your post would have been able (or should be able) to read
    > > your mind over the internet, figure out your intentions and missing
    > > information and then establish that your intentions are sound, fair,
    > > legitimate, level-headed and that your application requirements are all
    > > balanced, sensible and above any reproach?

    >
    > > > Since users a trained to press the K-Keys for help.

    >
    > > I read your post several times and still don't understand how or why the
    > > F1 key should be replaced by the "K-Keys" for help or why it is a good
    > > idea to replace the F1 key in an intranet application with the "K-keys".

    >
    > > Maybe one day there will be an original urbanist who will redesign color
    > > for street lights and he will use purple or gray for stop instead of red
    > > and he will choose yellow for going across street intersection instead
    > > of green and such colors will only apply to his town, not outside such town.

    >
    > > > It makes perfect
    > > > sense to override the Help menu of the browser with the Help Menu of
    > > > the application.

    >
    > > Your words up there. Not mine.

    >
    > > > Sincerely,
    > > > Blaine

    >
    > > Fair enough. Good luck.

    >
    > > Gérard
    > > --
    > > Using Web Standards in your Web Pages (Updated Dec. 2006)http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_...
     
    , Apr 10, 2007
    #5
    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. Jason
    Replies:
    2
    Views:
    2,573
    =?Utf-8?B?U3RldmUgSw==?=
    Jun 10, 2004
  2. =?Utf-8?B?RXJpYyBEYW4=?=

    Stopping ASP.NET application during Application_Start event

    =?Utf-8?B?RXJpYyBEYW4=?=, Jan 30, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    2,426
    MattC
    Feb 1, 2005
  3. Nathan Sokalski
    Replies:
    11
    Views:
    863
    Alessandro Zifiglio
    Jul 27, 2006
  4. Paul
    Replies:
    3
    Views:
    839
    Grant Merwitz
    Sep 12, 2006
  5. David

    Stopping a bubble event

    David, Apr 26, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    387
    bruce barker
    Apr 26, 2007
Loading...

Share This Page