Welcome to WebmasterWorld Guest from 54.225.31.78

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Reloading page by pressing enter

Language flash card site

     
8:46 am on Mar 18, 2006 (gmt 0)

10+ Year Member



I have always used a standard method to get my PHP sites to work and I am finding solving this one a bit of a mind bender. I would appreciate some help. It sounds simple but I am not getting any success.

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.

10:14 am on Mar 18, 2006 (gmt 0)

5+ Year Member



That requires some sort of javascript

<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.

7:31 pm on Mar 18, 2006 (gmt 0)

10+ Year Member



Thanks. As an old 6502 machine code programmer I recognise 13(hex 0D) as the ASCII code for enter so it should work on all platforms. (A9 OD 20 EE FF used to print a carrage return for me on my old system.)
Like most serious coders I run linux :-) I have tried it so far in Epiphany and Konqueror. The latter fails to operate it but I have encountered funny javascript problems with it before.

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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month