icecats11 / js-hotkeys

Automatically exported from code.google.com/p/js-hotkeys
0 stars 0 forks source link

F5 does not return false in IE #39

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hitting F5 in IE does not return false.

Does not work in IE:

$(document).bind("keydown","F5",function(){
    /* do something */
    return false;
});

Does work in IE:

$(document).bind("keydown","F5",function(){
    /* do something */
    if (navigator.appVersion.indexOf('MSIE') != -1) {
        event.keyCode = 0; event.returnValue = false; }
    return false;
});

Why is this not default or how can I do this better?

Original issue reported on code.google.com by mikebran...@gmail.com on 1 Jan 2009 at 5:20

GoogleCodeExporter commented 8 years ago
I've tested the following in IE 7 and didn't get the behaviour you describe. 
e.g. the
page was reloaded after my code was executed.

<html>
    <head>
        <script src="http://jshotkeys.googlepages.com/jquery-1.2.6.js"></script>
        <script
src="http://js-hotkeys.googlecode.com/files/jquery.hotkeys-0.7.8.js"></script>
    </head>
    <body>
    <script>
    $(document).bind("keydown","F5",
        function(event){
            alert("F5");
            event.keyCode = 0; 
            event.returnValue = false;
            return false;
        });

    </script>

Original comment by Afro.Sys...@gmail.com on 1 Jan 2009 at 5:57

GoogleCodeExporter commented 8 years ago
That is exactly my point, it shouldn't because you put return false in your 
function.

This will work in IE but not in FF:

<html>
    <head>
        <script src="http://jshotkeys.googlepages.com/jquery-1.2.6.js"></script>
        <script
src="http://js-hotkeys.googlecode.com/files/jquery.hotkeys-0.7.8.js"></script>
    </head>
    <body>
    <script>
    $(document).bind("keydown","F5",
        function(){
            alert("F5");
            event.keyCode = 0; 
            event.returnValue = false;
            return false;
        });
    </script>
</body>
</html>

This will work in FF but not in IE:

<html>
    <head>
        <script src="http://jshotkeys.googlepages.com/jquery-1.2.6.js"></script>
        <script
src="http://js-hotkeys.googlecode.com/files/jquery.hotkeys-0.7.8.js"></script>
    </head>
    <body>
    <script>
    $(document).bind("keydown","F5",
        function(){
            alert("F5");
            return false;
        });
    </script>
</body>
</html>

This will work in both:

<html>
    <head>
        <script src="http://jshotkeys.googlepages.com/jquery-1.2.6.js"></script>
        <script
src="http://js-hotkeys.googlecode.com/files/jquery.hotkeys-0.7.8.js"></script>
    </head>
    <body>
    <script>
    $(document).bind("keydown","F5",
        function(){
            alert("F5");
            if (navigator.appVersion.indexOf('MSIE') != -1) {
                event.keyCode = 0; 
                event.returnValue = false;
            }
            return false;
        });
    </script>
</body>
</html>

But what is the easiest way doing this? And shouldn't be part of the hotkeys 
script
to disable the key after catching? And browser independent?

Original comment by mikebran...@gmail.com on 1 Jan 2009 at 9:17

GoogleCodeExporter commented 8 years ago
jQuery itself should do all these for you

From: http://docs.jquery.com/Events/bind

To cancel a default action and prevent it from bubbling up, return false:
$("form").bind("submit", function() { return false; })

To cancel only the default action by using the preventDefault method.
$("form").bind("submit", function(event){
  event.preventDefault();
});

Let me know if it suites you.
Thanks for using hotkeys plugin

Original comment by Afro.Sys...@gmail.com on 1 Jan 2009 at 9:51

GoogleCodeExporter commented 8 years ago
Thanks, i haven't seen this page. But, after seeing it, the problem stays 
exactly the
same:

<html>
    <head>
        <script src="http://jshotkeys.googlepages.com/jquery-1.2.6.js"></script>
        <script
src="http://js-hotkeys.googlecode.com/files/jquery.hotkeys-0.7.8.js"></script>
    </head>
    <body>
    <script>
    $(document).bind("keydown","F5",
        function(event){
            alert("F5");
            event.preventDefault();
        });
    </script>
</body>
</html>

This works only for FF and not for IE. (IE7)

Original comment by mikebran...@gmail.com on 1 Jan 2009 at 10:33

GoogleCodeExporter commented 8 years ago
Neither of the codes works in IE as you described - That is totally omiting the
Refresh action. In my case, IE showed the alert, and then, reloaded the page.

This is similar to what I described at 
http://code.google.com/p/js-hotkeys/wiki/about

"Firefox is the most liberal one in the manner of letting you capture all 
short-cuts
even those that are built-in in the browser such as Ctrl-t for new tab, or 
Ctrl-a for
selecting all text. You can always bubble them up to the browser by returning 
true in
your handler.

Others, (IE) either let you handle built-in short-cuts, but will add their
functionality after your code has executed. Or (Opera/Safari) will not pass 
those
events to the DOM at all. "

Original comment by Afro.Sys...@gmail.com on 11 Jan 2009 at 2:57