PDA

View Full Version : Shouldn't this simple keycode script work?



tivaelydoc
07-05-2007, 05:20 PM
<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 k = (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?

Trinithis
07-05-2007, 06:23 PM
You forgot the question mark in your conditional operator. Plus, I thought it was


var k = (key.keyCode)? key.keyCode: key.which;

Also, add this to the first line of your event function:


if(!key) key = window.event;

Trinithis
07-05-2007, 06:30 PM
The main reason it did not work is because e.keyCode returns a number representation of the letter (uppercase too).

Try this:



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);

tivaelydoc
07-05-2007, 07:52 PM
Oh sweet thx. If I wanted to add



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?

Trinithis
07-06-2007, 12:30 AM
If I understand correctly....


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

tivaelydoc
07-06-2007, 08:26 PM
What about links like:


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

tivaelydoc
07-08-2007, 07:06 AM
Is this what I should do?:



<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(!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);
</script>

Trinithis
07-08-2007, 08:09 PM
Before I can help you, you need to explain to me exactly what you are trying to accomplish:



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


Also, you should take out the comment I had placed in the code.

tivaelydoc
07-08-2007, 08:42 PM
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/dynamicindex4/lightbox2/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:


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

Trinithis
07-09-2007, 12:25 AM
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


document.getElementById("iframeID").src = "newPage.html";


From the iframe


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.

tivaelydoc
07-09-2007, 03:59 AM
Ok thanks. The only problem is that I don't have a parent url because it is a image.