helper.js Generic methods
json.js JSON methods
server.js Server methods brought up to the client
kafka.js KAFKA-specific methods
All of the JavaScript is held in place by "kafka.htm".
<script type="text/javascript">
function draw_kafka() {
var how = {
'text_id' :'txt_length',
'wrap_id' :'div_wrap'
};
KAFKA.draw(how);
}
function page_load() {
// Bind the button "onclick" method
document.getElementById('btn_draw').onclick = draw_kafka.bind(this);
// Default KAFKA length
document.getElementById('txt_length').value = '6';
}
window.onload = page_load;
</script>
Function "page_load" is invoked when the page is rendered. This sets up the "onclick" event for the draw KAFKA button and defaults the number of characters to six.
var HESSE = (function() {
var private_property_1 = 'You cannot see this from outside HESSE';
var private_property_2 = 'HESSE shares this with you';
function private_method_1(parm) {
alert(private_property_1 + '\t\n' + arg); // Show the world one of my secrets and the passed parameter
return private_property_2; // Make private property visible (NOT public)
}
// Exposed (public) methods
return { // Bracket must be on same line to avoid JavaScript misunderstanding our intent
'public_method_1' : function(parm) { return private_method_1(parm); }
};
})();
The only method HESSE exposes is "public_method_1". It alerts a secret and returns another. This is the only interaction allowed. You cannot access any of its internal (private) properties or methods. For example:
var result = HESSE.public_method_1('arlo'); // VALID
var secret_1 = HESSE.private_property_1; // INVALID
HESSE.private_method_1('fred'); // INVALID
Let's examine KAFKA's "draw" method.
function draw_kafka(arg) {
// Expects
// arg
// .text_id ID of input for number of chars
// .wrap_id ID of KAFKA container
// Validate and save the number of chars, set the wrapper ID and set the callback function
var wlen = Math.min(Math.max(parseInt($(arg.text_id).value, 10), 6), 16), // min = 6, max = 16
chow = {
'num_letters':wlen,
'wrapper_id':arg.wrap_id,
'callback':kafka_watchdog
},
//
exknob; // Final var (prevents missing "," and ";"
document.getElementById(arg.text_id).value = wlen; // OPTIONAL: Visual feedback only
// The way we were...
invoked_with = arg;
get_kafka(chow);
}
We create a JSON var with the necessary arguments; we display the actual number of bimps; we save the original arguments for later; finally, we invoke the private method "get_kafka" with our JSON parameter.
"01100100101001011110100101001010010"
Subsequent letters - and the numbers zero through nine - look similar.
"01100"
"10010"
"10010"
"11110"
"10010"
"10010"
"10010"
The letter is more visible, but we can make it clearer by substituting two spaces for the zeroes and two "at" signs for the ones:
" @@@@ "
"@@ @@ "
"@@ @@ "
"@@@@@@@@ "
"@@ @@ "
"@@ @@ "
"@@ @@ "
When we display this in a 4-point type, it looks like a 10-point font character.
document.getElementById('OBJECT_ID').EVENT_NAME = FUNCTION_NAME.bind(this, 'PARAM_1', PARAM_2, ... PARAM_n);
//
// In practice, for the KAFKA "OK" button, it is bound like this:
//
document.getElementById('btn_ok').onclick = kafka_validate.bind(this, 'txt_kafka_letters', false);
I no longer have to worry about event handling. It just works.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (6)
Commented:
Looks cool... and thanks for your sharing. It tested successfully in FF3.5.x
However, when I tested in IE6.0, the captcha was not displayed correctly. Any idea? Thanks.
snap-captcha.jpg
Commented:
That said... I found this to be an interesting piece of code, and I agree that it would foil anyone who did not have the will or the expertice to write a decoding function. Your use of JSON and passing temporary structures as parameters is outside of my experience... I've learned something here. Thanks!
I voted Yes, above :-)
Commented:
* Very interesting article for the coding practices. I had a cursory read of Daniel Brockman's text, will read it in detail after a really good night's sleep ;)
* Some/many companies are still on IE6 for corporate upgrade policy reasons
I saw a new captcha the other day - the letters came flying into a box per letter and disappeared, one had to type them as they appeared. Looked like fun and also looked hard to ocr.
Some sites do need captchas or other bot defending stuff. The site I work on is pumping out hundred of GB a day and need to be able to block some types of requests from being automated.
Commented:
I am going to put it in a shopping cart admin page as a confirm big change thing, to make certain they really want that change.
Commented:
https://www.experts-exchange.com/articles/9849/Making-CAPTCHA-Friendlier-with-Simple-Number-Tests-or-PHP-Image-Manipulation.html
Given the advances in computer vision and machine learning, the old ways of CAPTCHA are rapidly becoming obsolete. Anyone with some basic CS skills and an NVidia GEForce card can do image recognition. Facebook can identify and tag your friends in your pictures. So "reading" a CAPTCHA image is well within the scope of modern computability. More and different methods are needed.
View More