Forum Moderators: coopster
I need to get a page to reload repeatedly on pressing enter. There is no user input form and the user will exit by clicking a link.
The page is a flash card type operation for a non-comercial site teaching people how to speak an endangered language. (It is has 20,000 people studying it a year according to the available statistics.) The idea is that the page creates
a randomish but gramatically correct sentence which the user then repeats to themselves before pressing enter to get the next sentence.
Could someone please suggest an outline of usable code. I have the flash cards working but currently have to click reload which is less than ideal.
<script language="JavaScript">function init() {
document.onkeydown = function(e) {
if (!e) e = window.event;
var code = e.keyCode;
if (code == 13) // 13 is Enter Key
window.location = self.location;
}}
window.onload = init;
</script>
The code says, when the page loads, run the init function. The init function sets an event based function, which says when someone presses a key, this is the function.
For keyboard based events, there's a keyCode property associated with the event, which is where e.keyCode comes from.
I'm not sure if it's different for Mac/Linux machines, but on Windows, 13 is the key code for the Enter button.
So in all, it says "when a user presses a key, determine what key they pressed. if they pressed the Enter key, go to a location. In this case, we're reloading so, self.location works.
I just put this code together real quick and tested in firefox 1.0 and IE6. Might not be fully cross-platform/cross-browser, but it's a starting point.
That is fine thanks a lot. For those people with a problem browser I can put a link on the page to help them get help and tell them to click on reload if all else fails.