views:

70

answers:

1
<body id="body" runat="server" onkeydown="return showKeyCode(event)">

Now whenever I hit a key, IE8 (or in comp mode) throws an exception directing to a problem in line x, which happens to be my body tag. How can I prevent that ? The JS code to my knowledge should be with IE comp. (works in Chrome) Moreover the code doesn't work in IE and Firefox (it doesn't block F5 and Enter)

--> Object expected

var version = navigator.appVersion;

function showKeyCode(e) {
    var keycode = (window.event) ? event.keyCode : e.keyCode;

    if ((version.indexOf('MSIE') != -1)) {
        if (keycode == 13) {
            event.keyCode = 0;
            event.returnValue = false;
            return false;
        }
    }
    else {
        if (keycode == 13) {
            return false;
        }
    }
}

Another problem I'm facing is with this simple JS in IE & FF (works in Chrome):

Nothing happens & --> Object expected

 <a onclick="ClearTextboxes();" title="Close" id="close" runat="server">Close</a>

....in script tags:
 function ClearTextboxes() {
     document.getElementById('<%= txtbox_name.ClientID %>').value = '';
     document.getElementById('<%= txtbox_email.ClientID %>').value = '';
     document.getElementById('<%= txtbox_content.ClientID %>').value = '';
     document.getElementById('<%= ResultMail.ClientID %>').style.display = 'none';

 }
+2  A: 

You have a lot of unnecessary code in your showKeyCode function. The following will do just as well; you're already guaranteed that e will refer to the key event object becuase of the way you're passing in event in the body's onkeydown attribute, and there's no need for any browser sniffing at all. return false is the correct way to prevent the browser's default action when using a DOM0 key handler, so no need for e.returnValue.

You'll have problems blocking F5 and unless you have a seriously good reason you shouldn't do it anyway, since the user is used to it performing a page refresh. If you change the function to the following then there's no reason why it shouldn't prevent the Enter key's default action:

function showKeyCode(e) {
    if (e.keyCode == 13) {
        return false;
    }
}
Tim Down
Don't block F5, use a onbefore unload handler to make sure the user knows they are going to lose changes, they can cancel the refresh if they want to. This will work for back button, close window, navigate away.I would strongly consider using jquery, ext-core, prototype which do the normalization of keyboard event data for you.The best reference with keyboard handling across platforms/browsers http://unixpapa.com/js/key.html
Juan Mendes
There's no need for a framework for this. My answer will work in all the major browsers released in the last decade.
Tim Down
I made a huge mistake when I started this project...I used XHTML-compliant script tags, instead of the usual <script></script> which seems to explain why these scripts were working in Chrome and in none of the other browsers...
dll32