how to implement the Konami easter egg in JavaScript
Here's a little weekend fun: a while back, word started spreading that Facebook had added an easter egg based on the old Konami code:
Specifically, they added a lens flare effect that only shows up while scrolling, and only after the above keystrokes have been entered in sequence, followed by the Enter key. For those who don't know (and haven't already clicked the link above to find out), this sequence was often used in old Konami video games to unlock something magical (extra lives, invincibility, etc.), and was so widely known that it's wormed its way into numerous other facets of popular culture.
Enabling this in JavaScript is actually easier than you might expect. Here's one possible implementation approach you could add to your own script:
var konami = (function(){
var sequence = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13];
var progressIndex = 0;
var easterEgg = function(){
/*
* Replace with your own easter egg code:
*/
alert("KONAMI'ED!!!");
};
return {
interceptKey: function(thisEvent){
var keyCode;
if (window.event) {
keyCode = thisEvent.keyCode;
}
else {
if (thisEvent.which) {
keyCode = thisEvent.which;
}
}
if (keyCode == sequence[progressIndex]) {
progressIndex++;
if (progressIndex == sequence.length) {
this.trigger();
}
}
else {
this.reset();
}
},
reset: function(){
progressIndex = 0;
},
trigger: function(){
easterEgg();
this.reset();
}
}
}());
Then just bind the interceptKey method to the onkeyup event of whatever element you want to contain the easter egg, e.g.:
<body onkeyup="konami.interceptKey(event)">
NOTE: In case it's not glaringly obvious, this only applies to client JavaScript. For all you XPagers out there, this wouldn't work as a server-side JavaScript function. Not only does SSJS not have a window object, the event model is entirely different from that of browser-based JavaScript.

Comments
Easter Day is the first Sunday after the full moon which happens upon, or next after the 21st day of March; and if the full moon happens upon a Sunday, Easter Day is the Sunday after." What kind of kid did I raise that misses a release by five months?
Posted by pops At 01:09:40 PM On 12/16/2009 | - Website - |
Posted by Tim Tripcony At 03:05:01 PM On 12/16/2009 | - Website - |
Posted by pops At 04:02:13 PM On 12/16/2009 | - Website - |