SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Javascript help

  1. #1
    Freelance Web Designer KeithMcL's Avatar
    Join Date
    Oct 1999
    Location
    Dublin, Ireland
    Posts
    1,125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all,

    Does anyone know if there is some javascript code that picks up on someone hitting the enter key?

    What I mean is, I have an image map where someone can enter information into an empty field and hit enter and it automatically redirects to another page.

    At the moment when someone hits enter the same page reloads. They need to click on a search button which is part of the image map to redirect them.

    I would prefer for them to be able to simply hit and enter and get it to redirect.

    Hope someone understands what I mean and has some code to do it

    Thanking you in advance.

  2. #2
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am unsure of the exact syntax, but to refresh someone else, perhaps....

    I remember something about an onKeyPress event that is fired. From there you can tell which key it is (using ASCII values, I believe).

    Also, there are things such as 'hot keys' (just my terminology, no relevence really) that you can set up which will make your form fire when someone presses enter. I am unsure of the syntax.

    Sorry I couldn't provide an accurate example, but please, everyone else post here! Thanks!

    Hope this helps, however little!
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  3. #3
    Irritability Defined
    Join Date
    Jul 2000
    Location
    80,000 feet below the surface
    Posts
    1,442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Goober is quite right.

    If you have a look at this script from DynamicDrive you'll see it's entirely possible. However, you need to find the correct ASCII number for hitting the 'Enter' key - something I'll leave to you

    The problem is, I'm not sure whether you can assign the Enter key, as it may conflict with other functions on the page (where you'd normally hit Enter).

    Good luck....
    My 2 Cents (or is that 2.2 Cents including GST?)

  4. #4
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I do know from experience that when coding, chr(13) and chr(10) equal a new line (the equivalent of enter). Perhaps either one of these would work in your predicament. E-Mail me if you do not find it soon. I have an ASCII reference at home. (God bless the old IBM programming books, eh? )

    Thanks for listenin' to me blab.
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  5. #5
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    Goober's right. To detect an enter, you use 13. Here's my example code:

    Code:
    <script>
    function getKey(e){
    	keyCode = navigator.appName=="Netscape"?e.which:event.keyCode;
    	if(keyCode == 13){
    		alert('You pressed "Enter"');
    	}
    }
    if(document.layers)document.captureEvents(Event.KEYPRESS)
    document.onkeypress = getKey;
    </script>
    aDog

    P.S.-This new interface is weird...

  6. #6
    Irritability Defined
    Join Date
    Jul 2000
    Location
    80,000 feet below the surface
    Posts
    1,442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, bingo! That's my thing learnt for the day.. Thanks aDog.
    My 2 Cents (or is that 2.2 Cents including GST?)

  7. #7
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like a tech times tip!

    Someone call Kevin!
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  8. #8
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    Here's actually the way I found that out (At first I thought it was 14, but wasn't sure...13's close to 14 ).

    Code:
    <script>
    function getKey(e){
    	keyCode = navigator.appName=="Netscape"?e.which:event.keyCode;
    	alert(keyCode);
    }
    if(document.layers)document.captureEvents(Event.KEYPRESS)
    document.onkeypress = getKey;
    </script>
    Then, you can find any keycode, and sometimes they're different for IE and NN.

    aDog

  9. #9
    Freelance Web Designer KeithMcL's Avatar
    Join Date
    Oct 1999
    Location
    Dublin, Ireland
    Posts
    1,125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the help so far, but I still haven't managed to get the script finished (still relatively new to javascript).

    At the moment when I enter something into the empty form field and press enter it still reloads the same page.

    I'm not sure how to get one function to call another.

    Here's the code I have so far:
    Code:
    <!-- this function displays a different div depending on the search string given -->
    function chooser(){
    
    if (document.form.name.value == "username1")
    {
    parent.location=username1.html';
    }
    if (document.form.name.value == "Username1")
    {
    parent.location=username1.html';
    }
    if (document.form.name.value == "")
     {alert("You did not enter a User Name.")}
    }
    Can someone tell me how I incorporate the pressing of the nenter button into this script, so that when the enter is pressed the script recognizes it, then reads the content of the form field and redirects to the appropriate username1.html page.

    Here's a link about "Javascript and your keyboard" that might help: http://www.wsabstract.com/javatutors...riptkey6.shtml

    The enter key does use the number 13, btw.

    Hope someone can help me out here

  10. #10
    Freelance Web Designer KeithMcL's Avatar
    Join Date
    Oct 1999
    Location
    Dublin, Ireland
    Posts
    1,125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it working

    The problem was that there was a form on the page also and the enter key was running the form instead of the javascript code, so I needed to add another function to tell it not to.

    Thanks for all your help folks, it was really appreciated.


Bookmarks

Posting Permissions

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