Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:46:04+00:00 2026-06-07T21:46:04+00:00

I found a simple code on the internet a while ago… can’t find the

  • 0

I found a simple code on the internet a while ago… can’t find the spesific site for it, but the code was made in 2005. So, I wondered if this was a good way of doing this:

This is in the HTML:

<body onload="document.body.focus();" onkeyup="return keypressed(event)">

When the key is up it calls this function in JavaScript:

function keypressed(e) {
    var intKey = (window.Event) ? e.which : e.keyCode;
    if (intKey == 13) { // 13 = enter key
        //something happens if the key is clicked
    }
        // here, you can add as many if sentences for key strokes as you want! (same as first)

return true;
}

What I mean is that the code is 7 years old (maybe more!) and I thought it might be a little or very outdated!

My questions:

  1. Is there a better way of doing this or is this still a good way? (I don’t want to use jQuery for this code, because I don’t want to use it just for one function)

  2. Do I really need this code: onload="document.body.focus();"? (I have not programmed this code, so I don’t know.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:46:06+00:00Added an answer on June 7, 2026 at 9:46 pm

    You don’t need the load handler, the page will be already focused after it loads, and/or if someone presses enter anywhere on the page it will be focused too.

    If you need the keyup handler depends on what you’d like to do with it. The code you show is an enter handler. If you want a text input field to act on typing enter, it’s more efficient to add the handler to that field.

    Furthermore, it’s better to do that from within your scripting (a part of unnobtrusive javascript usage). A keypress handler would be sufficient, because otherwise the enter value would become part of the input value. Something like:

    [someTextInput].onkeypress = function(e){
      e = e || event;
      if ( (e.which || e.keyCode) === 13 ){
         // do things
      }
    }
    

    or you can use addEventListener/attachEvent (See here).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That seems a very simple question but I don't found anything on internet (or
I found out the next issue in this simple code: let () = print_endline
Here in Apple's sample code I found this type of persistence storage. Can anyone
should be simple right? but have not found any anwsers :
I found this article about simple proxy server implemented in JAVA: http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm The code
I'm trying to compile a simple calculator example that I found in the internet
I've found a lot of different examples on the internet, but nothing seems to
Question is rather simple but I couldn't find nice & clean solution to my
I found the below code in the Internet, I'm trying to understand how Linux
I've found a simple solution somewhere on the internet to an identity class without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.