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.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question HELP WITH onkeydown()

    Goodevening

    function Keypressed()
    {
    window.location = "cart.html";
    }

    This is how I call the function:

    <p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html" onkeydown = "Keypressed()">here</a></p>


    It works fine but I can press any key and it goes to the linked page.
    I only want it to respond on the letter "v"


    How can I change this code so that when I press the letter "v" on the keyboard it access the link?

    Hoping to hear from someone?

  • #2
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try using

    onKeyDown="Keypressed(event.keyCode);"

    You can then test the value of keyCode in your function

    function Keypressed(keyCode)
    {
    if (keyCode==86)
    {
    window.location = "cart.html";
    }
    }

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Did you even know why your code didnt work?

    I was looking at your code, and well, i noticed, that you didnt pass any specific details to your function at the time of the functions activation.

    Goodevening

    function Keypressed()
    {
    window.location = "cart.html";
    }

    This is how I call the function:

    <p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html" onkeydown = "Keypressed()">here</a></p>


    It works fine but I can press any key and it goes to the linked page.
    I only want it to respond on the letter "v"


    How can I change this code so that when I press the letter "v" on the keyboard it access the link?
    You have defined a function above, but you have not given the function any values, or variables. For this reason, when you call your function, all it will do, is take a person to a web-file, since the web-file is the only thing assigned as a value to the property/object location. For this reason, if you want the function to respond to a very specific event, like the actual pressing of the V key on the keyboard, you will have to set up your function in such a way, that it will respond only to that event, and not to any other. Here's what you have:

    Code:
    <p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html" onkeydown = "Keypressed()">here</a></p>
    You have chosen to place your function inside a link tag. For this reason, when you get a visitor to click on the link, the only thing that will be activated is the function, even though, in your example, you have said, onKeyDown. You have the right event-handler, but the wrong set up: you are using a link tag to call a function.

    Additionally, when someone clicks on the link, they will go to "cart.html" because you have given that addy as a value to the href property of a tag. There's no reason why you should be having that if you are wanting a function to control the location property. This is why a visitor is always going to get the addy of "cart.html". To just use the link tag for the place of a function, but you do not want the link to act as an actual link, then you should be doing something like "href="#". If you dont do this, then the link will always act as a link. A function is generally used for when you want to seperate, and control specific lines of code, code that needs to stand on its own from other code. That's the whole reason for using a function, nothing more.

    Have you read any books, or gone to any sites?
    Last edited by SpiritualStorms; 07-13-2004 at 04:43 AM.
    LovesWar

  • #4
    New Coder
    Join Date
    Jul 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thank You Both for your Help!!

    Cheers

  • #5
    New Coder
    Join Date
    Jul 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Still need HELP with onkeydown()

    Attention: Carbootsale

    I tried your code it works but not when the page first opens.

    I have to click on the "here" link and then when i return to my order page then I can press the "v" key on the keyboard.

    It only works after I have gone to the shopping cart page and returned to my order page.

    Here is your code and it works only with the little proble described above:

    Code:
    onKeyDown="Keypressed(event.keyCode);"
    
    
    function Keypressed(keyCode)
    {
    if (keyCode==86)
    {
    window.location = "cart.html";
    }
    }
    It has something to do with the placement of the onKeyDown="Keypressed(event.keyCode);" event handler
    Last edited by HiEverybody; 07-13-2004 at 11:05 AM.

  • #6
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    document.onkeydown = keypressed;
    function keypressed(e) {
    	e = e || window.event;
    	var key = String.fromCharCode(e.keyCode).toUpperCase();
    	if(key == "V") {
    		window.location.href = "cart.html";
    	}
    }
    Code:
    <p align="center">Press 'V' to view your Shopping cart or click <a href="cart.html">here</a></p>
    dumpfi

  • #7
    New Coder
    Join Date
    Jul 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank You Very Much!!!!!

    You are super cool my friend and I thank you for your help.

    Your a lifesaver!!!!!


  •  

    Posting Permissions

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