Måden vi mødes på har ændret sig. Så hvorfor ser vores mødelokaler stadig ens ud?
Slettet bruger
08. maj 2001 - 12:05#1
The following script allows your visitors to reload your page simply by pressing \"r\":
<script language=\"JavaScript1.2\">
///if the user is using NS 4+ if (document.layers) document.captureEvents(Event.KEYPRESS) function reload_window(e){ if(String.fromCharCode(e.which)==\"r\") window.location.reload() }
///if the user is using NS 4+ if (document.layers) document.onkeypress=reload_window
</script>
Nothing too out of the ordinary here- we use e.which in combination with String.fromCharCode() to detect when \"r\" is pressed, and reload the window when that happens. Notice the object detection code document.layers, which is present to screen out NS 4+ browsers.
-Launch navbar example
As an even more practical example of the keyboard interacting with JavaScript, the following launches a navigational window (aka: remote) when the user presses \"a\" or \"A\":
<script language=\"JavaScript1.2\">
if (document.layers) document.captureEvents(Event.KEYPRESS) function reload_window(e){ if(String.fromCharCode(e.which)==\"a\"|| String.fromCharCode(e.which)==\"A\") navwin=window.open(\'nav.htm\',\'\',\'width=190,height=390\') }
if (document.layers) document.onkeypress=reload_window
</script>
Try pressing \"a\" now to see the script in action.
All of the above examples are intentionally made simplistic to allow you to focus on the fundamental technique to detecting and reacting to a key press. Having understood that, you can then go on to creating more complex key scripts, such as ones that detect when a certain word has been typed, advanced form validation scripts, and so on. But wait, we\'re not quite through with this subject matter just yet. In Part 2 of \"JavaScript and your keyboard\", we\'ll see how to detect and handle key presses in IE 4+. Stay tuned!
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.