Bad Javascript code
Results 1 to 3 of 3

Thread: Bad Javascript code

  1. #1
    Member
    Join Date
    Jun 2003
    Posts
    86

    Bad Javascript code

    I most likely just made a stupid error somewhere, but i cant find it...
    I just started learning javascript yesterday and i wrote the following program to find the keycodes for the keyboard

    <HTML>
    <HEAD>
    <TITLE>
    Javascript Practice
    </TITLE>
    <script language = "javascript" type = "text/Javascript">
    <!--
    document.onKeydown = keyHit();
    function keyHit()
    {
    key = window.event.keyCode;
    document.write("You pressed Keycode " + key + "<BR><BR>");
    }
    //-->
    </script>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

    might someone tell me what is wrong with it? i run internet explorer
    You laugh because im different, i laugh because your all the same.

  2. #2
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,408
    Okay, first of all you need to put the onKeyDown event in your body tags IIRC (it's always worked this way for me anyway). Next, if you use document.write, it'll only work once (or at least that's what happened when I tried it) so you could consider using the alert() function instead. Anyway, here's the code I hacked together:

    Code:
    <html>
    <head>
    <title>Javascript Practice</title>
    <script language = "javascript" type = "text/javascript">
    <!--
    
    function keyHit()
    {
    	key = window.event.keyCode;
    	alert("You pressed Keycode " + key);
    }
    
    //-->
    </script>
    </head>
    <body onKeyDown="keyHit();">
    </body>
    </html>
    Hope this helps, feel free to ask if you have problems with the above code.
    Paul Waring - Web site design and development.

  3. #3
    Member
    Join Date
    Jun 2003
    Posts
    86
    Hey thanx that definately worked. I'm a newbie to this web programming thing and the book i read didn't tell me to put it into the body tag. Thanx
    You laugh because im different, i laugh because your all the same.

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides