Konami Code Easter Egg

  • follow us in feedly
Published March 31, 2013 by Brad Knutson
Konami Code Easter Egg

In honor of Easter 2013, I’ve decided to write up a post about perhaps my favorite game and web easter egg of all time, the Konami Code.

If you’re not familiar with the Konami Code, here is Wikipedia’s description of it:

The Code is a cheat code that appears in many Konami video games, although the code also appears in some non-Konami games.

During the title screen before the game demo begins, the player could press the following sequence of buttons on the game controller to enable the cheat:

up up down down left right left right b a

Using Javascript, we can accept a user hitting this key sequence and then perform any action we’d like. On a few of my client’s sites, I’ve implemented a script that will accept the Konami code and redirect the user to a discount page.

Here is the version of the script that I use.

<script language="javascript" type="text/javascript">
	function onKonamiCode(fn) {
		var codes = (function(){
				var c = [38,38,40,40,37,39,37,39,66,65];
				onKonamiCode.requireEnterKey && c.push(13);
				return c;
			})(),
			expecting = function(){
				expecting.codes = expecting.codes || Array.apply({}, codes);
				expecting.reset = function() { expecting.codes = null; };
				return expecting.codes;
			},
			handler = function(e) {
				if (expecting()[0] == (e||window.event).keyCode) {
					expecting().shift();
					if (!expecting().length) {
						expecting.reset();
						fn();
					}
				} else { expecting.reset(); }
			};
		window.addEventListener ?
			window.addEventListener('keydown', handler, false)
			: document.attachEvent('onkeydown', handler);
	}
	onKonamiCode.requireEnterKey = false;
	onKonamiCode(function(){
		window.location="http://example.com/special/url/";
	});
</script>

Reference URL

We don’t necessarily have to redirect the user to another page, we can do anything, like show a previously hidden div, play an audio track, etc. The possibilities are endless, have fun with it!

Happy Easter!!

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date

Subscribe

Topics

See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">