ASP.NET Custom Server Controls: WordHack Control - JavaScripts: the heart of WordHack
(Page 4 of 5 )
Until now, we have seen only the ASP.NET custom control code, but didn’t go through the JavaScript code at all. Now we shall go through the method “getJS4WordHack”.
If Me.Word2Hack.Trim = "" Then
Return ""
End If
Dim js As String
js = "<SCRIPT LANGUAGE=""JavaScript"">"
js &= "var SpecialWord = '" & Me.Word2Hack.Trim & "',"
js &= "SpecialUrl = '" & Me.RedirectURL.Trim & "',"
js &= "SpecialLetter = 0;"
js &= "function getKey(keyStroke) {"
js &= "var isNetscape=(document.layers);"
js &= "var eventChooser = (isNetscape) ? keyStroke.which : event.keyCode;"
js &= "var which = String.fromCharCode(eventChooser).toLowerCase();"
js &= "if (which == SpecialWord.charAt(SpecialLetter)) {"
js &= "SpecialLetter++;"
js &= "if (SpecialLetter == SpecialWord.length) window.location = SpecialUrl;"
js &= "}"
js &= "else SpecialLetter = 0;"
js &= "}"
js &= "document.onkeypress = getKey;"
js &= "</script>"
Return js
First of all (from the above code), I am checking whether there exists any “Word2Hack” which has been provided or not. If it is not provided, I simply skip the JavaScript (because there exists nothing to really hack). Once the “Word2Hack” property is assigned to a word (something like “logout”), the JavaScript gets emitted. The above JavaScript mainly captures any key press using the “onkeypress” event of the “document” object. Once any key gets pressed, it executes the “getKey” function which actually checks to see whether the successive key we pressed matches up with “Word2Hack” or not. If so, we immediately redirect it to the value available in the property “RedirectURL”.
Next: What is the WordHack Designer class? >>
More ASP.NET Articles
More By Jagadish Chaterjee