Results 1 to 6 of 6

Thread: onkeypress as attribute of body tag

  1. #1
    Join Date
    May 2007
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default onkeypress as attribute of body tag

    i'd like to test to see if a user has pressed a key combination like this...

    Code:
    <script type="text/javascritpt">
    function key(e) {
         if (e.keyCode == 18) {
              alert('whatever');
         }
    }
    </script>
    <body onkeypress="javascript:key(event);">
    blah blah blah
    </body>
    is this smart though? this page will have many text areas and text fields on it as well. is it too much to ask the browser to evaluate the value of a keystroke each time a key is pressed?

    any guidance would be much appreciated. thanks!

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Well, it is actually good. I would recommend taking the javascript: part out of your event handler. You also need a head tag. This wouldn't mess with the page much, though (if at all, not enough to be bad). Yeah. It's okay.

    -magicyte

  3. #3
    Join Date
    May 2007
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    thanks =)

    i forgot to put the head tags in

    however, i did not know if was bad to include javascript:

    i guess it is kind of redundant, but does it cause issues? i've just gotten in the habit (which might be a bad one) of putting that in front of everything that's referring to js. should i leave it out of all event handlers and javascript delegates?

  4. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Leave it out of event handlers, but leave them in href="" s. If you want to activate a function or javascript w/in a link, leave the javascript: part. Also, I don't think it would effect it, but do it just in case. Href's are automatically defaulted to URLS. If you use javascript:, it then converts to javascript to call functions. Event handlers are defaulted to client-scripting code.

    -magicyte

  5. The Following User Says Thank You to magicyte For This Useful Post:

    ahs10 (10-24-2008)

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    onkeydown is more reliable than onkeypress. Also, if you want backward compatibility, check e.which as a fall back:

    Code:
    var key = function (e) {
    e = e || window.event;
    var k = e.keyCode || e.which;
         if (k == 18) {
              alert('whatever');
         };
    };
    document.onkeydown = key;
    No need for an event attribute in the body tag.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Aye. I didn't notice he/she used onkeypress. onkeydown and onkeyup are your standard event handlers for pressing keys.

    -magicyte

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •