#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    168
    Rep Power
    22

    How to read javascript files?


    Hi guys,

    I need your help, I have a problem with my javascript where I am trying to access to another javascript file to get things right and working but it is not working when I press on the keyboard up and down arrow buttons.

    here is the html code:

    Code:
    <html>
    <body>
    
    
    <style type="text/css">
     
    body {background:url('/images/blue_background.jpg') no-repeat center center fixed;}
     
    </style>
    
    <body style="overflow: hidden">
    
    <script>
      <script type="text/javascript" src="test.js"></script>
      <script type="text/javascript">
    
    </script>
    </body>
    </html>

    here is the test.js file

    Code:
    document.onkeydown = checkKey;
    function checkKey(e) {
    
    if(key == 38)
    {
       alert('up arrow are working');
    }
    
    if(key == 40)
    {
       alert('down arrow are working');
    }

    Any advice would be much appreicated.

    thanks in advance
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Your test.js file is missing a }
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    168
    Rep Power
    22
    can you post the correct code to make it work?
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    I am physically capable of posting the correct code, but I would rather have you fix it and post the correct code.

    Also, in your HTML you have an errant <script> tag; you can't have a <script> tag inside another <script> tag:
    Code:
    <script>
      <script type="text/javascript" src="test.js"></script>
      <script type="text/javascript">
    
    </script>
    And in your checkKey function you never define the 'key' variable. The example at the very bottom of this page shows how to get the key that was pressed: https://developer.mozilla.org/en-US/.../KeyboardEvent
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    168
    Rep Power
    22
    I have checked everything, but it is not working. I have attached the zip file so you will know why it is not working.

    thanks in advance
    Attached Files
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Good, you fixed both of the problems mentioned in #4.

    The only outstanding problem now is that you have more opening braces ({) than closing braces (})
    Code:
    document.onkeydown = function(ev) { // 1 opening	
    var key;
    ev = ev || event;
    key = ev.keyCode;
    alert ("Keycode = " + key);  // for testing
    
    function checkKey(e) { // 2 opening
    
    if(key == 38)
    { // three opening
       alert('up arrow are working');
    } // one closing
    
    if(key == 40)
    { // four opening
       alert('down arrow are working');
    } // two closing
    } // three closing
    You have four opening braces, but only 3 closing braces; which means that somewhere in your code you are missing a }
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo