SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2001
    Location
    Michigan
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How I can disable the Enter key in Netscape?

    Here is a little JavaScript that disables the Enter key. Unfortunately it only works in MSIE but I need it also for Netscape. Anyone knows how to do this?

    Code:
    function onKeyPress () { 
    var keycode; 
    if (window.event) keycode = window.event.keyCode; 
    else if (e) keycode = e.which; 
    else return true; 
    if (keycode == 13) { 
    alert("Please click on the \"Go To Date\"-Button to refresh this page."); 
    return false 
    } 
    return true 
    } 
    document.onkeypress = onKeyPress;
    trmweb

  2. #2
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    trmweb...

    I'm not sure what exactly you are trying to do but when someone disables keys on my keyboard I leave the site. I personally think that it is bad judgement to have to disable part of the users computer just to get the effect you want.

    just my .02 cents.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  3. #3
    SitePoint Member
    Join Date
    Apr 2001
    Location
    Michigan
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    valid point, unless...

    The reason for this is quite simple:

    if you go to http://www.media-mba.unisg.ch/uni/mb...edule?Openform you are presented a form with 1 field. In that field you enter a date and the schedule for the Executive MBA Programme for that week will be shown. The problem is that to refresh the fields you have to press the button there. The Enter-Key (on a form with one field) normally has the function to send to form alas saving the document on the Notes server. It DOES NOT UPDATE the fields.

    I'm looking for a code solution that works for both IE & Netscape. The following doesn't seem to work either
    Code:
    <html>
    <head>
    <script language="JavaScript1.2">
    <!--
    function netscapeKeyPress(e) {
    if (e.which == 13)
    alert("enter pressed");
    }
    
    function microsoftKeyPress() {
    if (window.event.keyCode == 13)
    alert("enter pressed");
    }
    
    if (navigator.appName == 'Netscape') {
    window.captureEvents(Event.KEYPRESS);
    window.onKeyPress = netscapeKeyPress;
    }
    //-->
    </script>
    </head>
    <body onKeyPress="microsoftKeyPress()">
    text body - test by pressing the enter key...
    </body>
    </html>
    trmweb

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its still a valid point, because disabling the enter key is not the solution. Obviously the problem is an issue with the script that updates the data. It is probably looking for a variable that is the same name as your submit button, so if the user presses enter then that var does not get passed to the script for processing. You can fix this by adding a hidden field with the name that the script is looking for. For instance if your form has:

    <input type="submit" name="whatever" value="submit">

    Change it to

    <input type="hidden" name="whatever" value="submit">
    <input type="submit" name="somethingelse" value="submit">

    That way whether they click enter or the button the same vars will get passed.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Member
    Join Date
    Apr 2001
    Location
    Michigan
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks freddydoesphp!
    I think you addressed the real issue. I appreciate it!
    trmweb

  6. #6
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although you may not need to detect an event for this solution for future reference Netscape browsers require you to "capture" the event before you can do anything with it where Internet Explorer does not. That was one reason why the code did not work. Also Netscape uses a little bit different way to pass the event to the function. Here is some code that works in Netscape 4+, Netscape 6 and Internet Explorer 5 browsers. It detects When a key has been pressed and then displays the key and its ascii code. You will also notice that I place focus to the window after the alert, this is because Netscape 6 requires that so that you can detect the next key. Hope this helps and good luck.
    <html>
    <head>
    <title>TEST Key Press</title>
    <script language="JavaScript">

    IE5=document.all? 1:0
    if(!IE5) // Must capture the event if it is NS6.0 and NS4.0+
    {
    document.captureEvents(Event.KEYPRESS)
    }
    document.onkeypress = testKey; // when a key is pressed perform testkey function
    function testKey(e)
    {
    /* Grab the ascii code of the key that was pressed */
    whKey = !IE5? e.which:event.keyCode;
    /* Display the key and its ascii code */
    alert("Key is:"+String.fromCharCode(whKey)+" \n Ascii is:"+whKey)
    self.focus()
    }
    </script>
    </head>
    <body onLoad="self.focus()">
    </body>
    </html>


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
  •