5

I want to load these two files

<link rel="stylesheet" type="text/css" href="sewmuchcss.css">
<script type="text/javascript" src="sewmuchjs.js"></script>        

in my header only when a user presses "up, up down, down, left, right, left, right." How would I go about accomplishing this? Would I use jquery, or javascript. What would I have to do? Thanks in advance for any help or answers.

4 Answers 4

6

I would use jQuery, and you can use the following algorithm to check for the multiple keys in a row:

var keysPressed = [];
                       //  U,  U,  D,  D,  L,  R,  L,  R
var MAGIC_KEY_SEQUENCE = [ 38, 38, 40, 40, 37, 39, 37, 39 ]

$('body').on('keydown',function(e){
     var code = (e.keyCode ? e.keyCode : e.which);

     keysPressed.push( code );

     if ( keysPressed[ keysPressed.length - 1 ] == MAGIC_KEY_SEQUENCE[ keysPressed.length - 1 ] )
     {
       // so far so good

       if ( keysPressed.length == MAGIC_KEY_SEQUENCE.length )
       {
         // all keys were pressed in the right order!
         alert( 'hurray!' );

         $('<link/>').attr({
             rel:'stylesheet',
             type:'text/css',
             href:'sewmuchcss.css'}).appendTo('head');
         $.getScript('sewmuchjs.js');
       }
     }
     else
     {
       // something didn't match, so reset the list
       keysPressed = []       
     }
})​

Play with it here: http://jsfiddle.net/japanick/vfRqk/

Sign up to request clarification or add additional context in comments.

5 Comments

hey Nick, I'm testing it out now! Thanks so much
Hey Nick, the alert and everything appears fine, however the script runs automatically when you load the page. How do i make it run only after the alert appears!
@KiaDull Have you removed those two tags from your header? They should not appear in the header, they will only be referenced by the Javascript here. One easy test is to load your page and view source. If you find "sewmuchcss" or "sewmuchjs" as <link ...> or <script ...> tags, you are incorrectly loading it still on page load.
im using jquery jquery-1.4.1.min.js for the script i need to run, do i need to use a later version of jquery to make this work? the script i need to run doesn't work with later versions of jquery :(
I updated the jsfiddle to use jq 1.4.1.min and I had to change the "on" to a "bind". jsfiddle.net/japanick/vfRqk But then it worked fine for me! $('body').bind('keydown', ...
1
$('#target').keypress(function(){
     var code = (e.keyCode ? e.keyCode : e.which);
     if(code == 21) // write your preferable keycodes here
     {
          $('<link/>').attr({ rel: 'stylesheet', type: 'text/css' href:'sewmuchcss.css' }).appendTo('head');
          $.getScript('sewmuchjs.js');
     }
})

fiddle: http://jsfiddle.net/pwunq/

5 Comments

sorry, I don't quite follow can you elaborate a bit
and also, that's just for one key how would i do multiple 'up down up down left right left right' Thanks for the help if you know how :)
You will need jQuery included for this approach, perhaps that explains the errors you were getting?
@Nick My errors was because of $('<script>/<script>') ... and I replaced it with $.getScript
0

You can use jquery hotkeys- https://github.com/jeresig/jquery.hotkeys

On key event you can add -

<link rel="stylesheet" type="text/css" href="sewmuchcss.css">
<script type="text/javascript" src="sewmuchjs.js"></script>  

this js and css by

$('head').append('<link rel="stylesheet" type="text/css" href="sewmuchcss.css">');  
$('head').append('<script type="text/javascript" src="sewmuchjs.js"></script>');  

Comments

0

you can access these 2 files using runat="server" and give id="likeme" for example -

 <link rel="stylesheet" type="text/css" href="sewmuchcss.css" runat="server" id="likeme" >

2 Comments

How does this answer the question for only loading those files after a sequence of keypresses?
on keypress from serverside, you can call and set the href

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.