Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Shouldn't this simple keycode script work?

  1. #1
    Join Date
    Jul 2007
    Posts
    40
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Shouldn't this simple keycode script work?

    PHP Code:
    <script language="JavaScript">
    var 
    captcha = new Array();
    var 
    edits = new Array();

    var 
    cheatCode '3838404037393739989713';
    var 
    cheat '';
    document.keypress(function(key) {
        if (
    cheat.length cheatCode.length) {
            var 
    = (key.keyCode == 0)  key.charCode key.keyCode;
            
    cheat cheat String(k);
            if (
    cheat == cheatCode) {
               
    alert("example") } 
        }
    });
    </script> 
    ( source: http://digg.com/js/19/jquery-comments.js )
    I got this code from the digg.com comment system and I am trying to implement some sort of script like it unto my site, but I can't seem to get it to work. I am very new with js, so can someone help me out?

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    You forgot the question mark in your conditional operator. Plus, I thought it was

    Code:
    var k = (key.keyCode)?  key.keyCode: key.which;
    Also, add this to the first line of your event function:

    Code:
    if(!key) key = window.event;

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    The main reason it did not work is because e.keyCode returns a number representation of the letter (uppercase too).

    Try this:

    Code:
    var cheatCode = "all your base are belong to us".toUpperCase();
    var codeIndex = 0;
    
    function executeCheat() {
    	alert("h4x3d!!!1!11");
    	}
    
    function codeHandler(e) {
    	if(!e) e = window.event;
    	var k = e.keyCode? e.keyCode: e.which;	//Assuming e.which is correct
    	if(!(k==cheatCode.charCodeAt(codeIndex++))) {
    		codeIndex = 0;
    		return;
    		}
    	else if(codeIndex==cheatCode.length) executeCheat();
    	}
    
    document.addEventListener? document.addEventListener("keydown", codeHandler, false): document.attachEvent("onkeydown", codeHandler);
    Last edited by Trinithis; 07-05-2007 at 06:47 PM.

  4. #4
    Join Date
    Jul 2007
    Posts
    40
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Oh sweet thx. If I wanted to add

    PHP Code:
    onClick="h4x3dwin=dhtmlwindow.open('h4x3d!!!1!11', 'iframe', 'example.com', 'h4x3d!!!1!11', 'width=1337px,height=666px,center=1'); return false" 
    to replace the alert("h4x3d!!!1!11")
    What would I have to change it to?
    Last edited by tivaelydoc; 07-05-2007 at 07:58 PM.

  5. #5
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    If I understand correctly....
    Code:
    function executeCheat() {
    	h4x3dwin=dhtmlwindow.open('h4x3d!!!1!11', 'iframe', 'example.com', 'h4x3d!!!1!11', 'width=1337px,height=666px,center=1');
    	}

  6. #6
    Join Date
    Jul 2007
    Posts
    40
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    What about links like:

    PHP Code:
    <a href="example.jpg" rel="lightbox[1337]" title="h4x3d!!!1!11">all your base are belong to us</a

  7. #7
    Join Date
    Jul 2007
    Posts
    40
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Is this what I should do?:

    PHP Code:
    <script language="JavaScript"
    var 
    cheatCode "example".toUpperCase();
    var 
    codeIndex 0;

    function 
    executeCheat() {
    iloveyourubywin=dhtmlwindow.open(
    "example.swf" rel="lightbox" title="h4x3d!!!1!11");
        }

    function 
    codeHandler(e) {
        if(!
    ewindow.event;
        var 
    e.keyCodee.keyCodee.which;    //Assuming e.which is correct
        
    if(!(k==cheatCode.charCodeAt(codeIndex++))) {
            
    codeIndex 0;
            return;
            }
        else if(
    codeIndex==cheatCode.lengthexecuteCheat();
        }

    document.addEventListenerdocument.addEventListener("keydown"codeHandlerfalse): document.attachEvent("onkeydown"codeHandler);
    </script> 

  8. #8
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Before I can help you, you need to explain to me exactly what you are trying to accomplish:

    Code:
    iloveyourubywin=dhtmlwindow.open(
    "example.swf" rel="lightbox" title="h4x3d!!!1!11");
    Also, you should take out the comment I had placed in the code.

  9. #9
    Join Date
    Jul 2007
    Posts
    40
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Well, i know, i'm going to edit it. I'm just copying and pasting temporally. But on this lightbox 2 script, you just add rel=lightbox and it opens in a nifty iframe.

    http://www.dynamicdrive.com/dynamici...box2/index.htm

    But I want it to open when I press a certain keycode, like the last one, but since the coding, I couldn't figure out what to do.

    Normally the link looks like this:
    PHP Code:
    <a href="example.jpg" rel="lightbox[1337]" title="h4x3d!!!1!11">all your base are belong to us</a
    but i want it not to open from a click but a keypress like the last one.

  10. #10
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Haven't worked with frames in a while, but if I recall correctly, you could do something like this.

    From the parent of the iframe
    Code:
    document.getElementById("iframeID").src = "newPage.html";
    From the iframe
    Code:
    window.parent.document.getElementById("iframeID").src = "newPage.html";
    But I might be wrong.

    If it doesn't work, someone familiar with frames would have to help you.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •