Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <script>
    <script type="text/javascript" src="test.js"></script>
    <script type="text/javascript">
    </script>

    You cannot place a script within another set of script tags. And the <script> and </script> tags must match.

    You have not defined key.

    Here you are:-


    Code:
    <script type = "text/javascript">
    
    document.onkeydown = function(ev) {	
    var key;
    ev = ev || event;
    key = ev.keyCode;
    alert ("Keycode = " + key);  // for testing
    
    if (key == 38) {
    alert ("You pressed the UP ARROW key");
    }
    if (key == 40) {
    alert ("You pressed the DOWN ARROW key");
    }
    
    }
    </script>
    Naturally you may place this script in an external .js.file.

    I'm going to get a picture organised of them playing with their balls. - Reporter, Surrey Advertiser getting excited about Oxshott Petanque club.
    Last edited by Philip M; 03-16-2013 at 06:32 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    <script>
    <script type="text/javascript" src="test.js"></script>
    <script type="text/javascript">
    </script>

    You cannot place a script within another set of script tags. And the <script> and </script> tags must match.

    You have not defined key.

    Here you are:-


    Code:
    <script type = "text/javascript">
    
    document.onkeydown = function(ev) {	
    var key;
    ev = ev || event;
    key = ev.keyCode;
    alert ("Keycode = " + key);  // for testing
    
    if (key == 38) {
    alert ("You pressed the UP ARROW key");
    }
    if (key == 40) {
    alert ("You pressed the DOWN ARROW key");
    }
    
    }
    </script>
    Naturally you may place this script in an external .js.file.

    I'm going to get a picture organised of them playing with their balls. - Reporter, Surrey Advertiser getting excited about Oxshott Petanque club.
    thanks for that, i have input the code in an external .js file. when i tried to test on my website, it doesn't work.

    any idea?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by mark103 View Post
    thanks for that, i have input the code in an external .js file. when i tried to test on my website, it doesn't work.

    any idea?
    Well, it works for me.

    Is the file in the correct directory?
    Have you made sure that there are no HTML tags in the .js file?
    Have you eliminated all typos?

    Try the script in-line in your webpage - if/when it works move it to an external file.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    that will only work inb the html document even not in the external .js file

    please post an example code that work for you in a html document and in the external .js file.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by mark103 View Post
    that will only work inb the html document even not in the external .js file

    please post an example code that work for you in a html document and in the external .js file.
    The code I posted in post #2 works just fine for me in an external file.

    You are obviously doing something wrong.

    Are you sure that you have removed the HTML tags <script> and </script> from the external file?

    Place the call to the external file right in front of the </body> tag.

    Code:
    <script type="text/javascript" src="test.js"></script>
    </body>
    Last edited by Philip M; 03-16-2013 at 07:31 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i tried this and it don't work, i have not done something wrong, you must be posting the wrong code or misunderstood what i am trying to achieve.

    Code:
    <html>
    <body>
    
    
    <script type="text/javascript" src="test1.js"></script>
     <script type="text/javascript">
    
    </body>
    </html>

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by mark103 View Post
    i tried this and it don't work, i have not done something wrong, you must be posting the wrong code or misunderstood what i am trying to achieve.

    Code:
    <html>
    <body>
    
    
    <script type="text/javascript" src="test1.js"></script>
     <script type="text/javascript">
    
    </body>
    </html>

    Please do not tell me that I am posting the wrong code.

    It is obvious from the above what is wrong - but in view of your attitude you can find it yourself. I have done my best to help you.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,078
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Quote Originally Posted by mark103 View Post
    i tried this and it don't work, i have not done something wrong, you must be posting the wrong code or misunderstood what i am trying to achieve.

    Code:
    <html>
    <body>
    
    
    <script type="text/javascript" src="test1.js"></script>
     <script type="text/javascript">
    
    </body>
    </html>
    Why do you have the second <script> tag without a closing tag?

    Also make sure you permissions are set correctly on your server to access the files.

    You might want to turn your hand around before you start pointing the blame!

  • #10
    New Coder
    Join Date
    Oct 2012
    Posts
    55
    Thanks
    26
    Thanked 0 Times in 0 Posts
    shouldn't the javascript tags be in the header section and not the body for this one?

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,634
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Mindphaser View Post
    shouldn't the javascript tags be in the header section and not the body for this one?
    If the script is testing whether to display this page or switch to a different page (or the same page outside of frames) then it goes in the head. All other JavaScript goes just before the </body> tag so that the HTML of the page loads first.

    99.99% of scripts belong at the bottom of the page so that the page can load faster and the script can run sooner.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Please do not tell me that I am posting the wrong code.

    It is obvious from the above what is wrong - but in view of your attitude you can find it yourself. I have done my best to help you.
    I am sorry but I found there is nothing wrong with my code.

    I have attached the html and .js document for you to take a look and test it.
    Last edited by mark103; 06-18-2013 at 04:00 PM.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    Maybe you should try actually *READING AND TRYING* the answers you get.

    Your code is totally bogus. It does *NOT* match what Philip showed you.

    And you ignored what everybody else told you.

    I really should NOT do this, because I will bet you will ignore me, too. But here is your code *CORRECTED*:

    *** file: test1.js ***
    Code:
    document.onkeydown = function(ev) 
    {	
        var key;
        ev = ev || event;
        key = ev.keyCode;
        alert ("Keycode = " + key);  // for testing
    
    function checkKey(e) {    
    
        if(key == 38)
       {
           alert('up arrow are working');
       }
       if(key == 40)
       {
           alert('down arrow are working');
       }
    }
    *REMOVE* the line in red. It is WRONG WRONG WRONG. Completely bogus.

    *** file: test.html ***
    Code:
    <html>
    <body>
    <script>
    </script>
    <script type="text/javascript" src="test1.js"></script>
    </body>
    </html>
    The code in red is DOING NOTHING. It doesn't hurt, but it is USELESS.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Regular Coder
    Join Date
    Sep 2008
    Posts
    115
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much for your help, you should have said earlier about removing the "function checkKey(e) {" and "<script></script>" which you didn't, so don't worry about it now as i got it removed and it is working right now

    By the way, I want to use "<script></script>" for javascript, so would it work if i use without them??

  • #15
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,078
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by mark103 View Post
    Thank you very much for your help, you should have said earlier about removing the "function checkKey(e) {" and "<script></script>" which you didn't, so don't worry about it now as i got it removed and it is working right now

    By the way, I want to use "<script></script>" for javascript, so would it work if i use without them??
    As Old Pedant said, as they are currently coded, they are useless.
    If you have a question like the last one, the easiest test would be to remove them yourself and see what happens.

    If it doesn't work, then you have either removed the wrong ones
    or he was correct: They are useless as you have coded them currently!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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