| TechTip: JavaScript Made Easy, Part II—Is Caps Lock On? |
|
|
|
| Tips & Techniques - Scripting | |
| Written by Jan Jorgensen | |
| Thursday, 11 October 2007 | |
|
With JavaScript, you don't have to reinvent the wheel. Someone has always been there before you. Sometimes you discover things that make you say, "Well, that's clever!"
Copy all the text to an empty HTML document, save it, and test it. Of course, it works perfectly! Now you just have to copy it to your RPG-CGI program or PHP script or whatever else you wrote your Internet application in. The job is done, and Mr. and Ms. Clumsy will never waste your time on that subject again! But there could be some improvements to the JavaScript. For example, I do not like "alerts." I'd rather see a true message. So let's look at the function called checkCapsLock:
function checkCapsLock( e ) {
incorrectly,\nyou should press Caps Lock to turn it off.';
therefore, Caps Lock is on
therefore, Caps Lock is on
) {
The first part is surely where all the key-press testing is done, and we will not touch that.
"document.getElementById('DivExample').innerHTML="Good Afternoon";"
Now, replace the "Good Afternoon" with the variable called myMsg. And do the same after the next alert. Then, comment out the alerts by placing two slashes (//) in front of them. Your code should now look like this: // Upper case letters are seen without depressing the Shift key,
therefore Caps Lock is on
therefore Caps Lock is on Now, you just need the container called DivExample to hold the text. So we'll just add a new table at the bottom of the page, and it should look like this:
<!-- Make message look good -->
<p> <table border="0"> <tr> <td> <div id="DivExample"></div> </td> </tr> </table> So now you have the following. (Please note that I have added a header and a <center> to make it look just a little better, but that should not spoil the joy.)
<HTML>
password incorrectly, you should press Caps Lock to turn it off.</b>';
key, therefore Caps Lock is on
key, therefore Caps Lock is on myShiftKey
) { Save it and test. It should work perfectly except for one thing: The message is not removed when you deactivate Caps Lock. So go back to the script and find where the message is displayed. Insert an "else," which will blank out the <div> container if the two previous if's where not executed. Enter the following line:
} else {
document.getElementById("DivExample").innerHTML=' '; So now you have this: // Upper case letters are seen without depressing the Shift key,
therefore Caps Lock is on
therefore Caps Lock is on myShiftKey
) {
I just use the to insert a blank. Save and test. Voila! It works like a charm! |
|
|
Last Updated ( Friday, 07 December 2007 ) |
|
| Guest.Visitor |
TechTip: JavaScript Made Easy, Part II—Is Caps Lock On?
Oct 28 2007 21:19:00 Check password. <p>If correct, allow in; else, <p>Check for all caps. <p>Send appropriate message.
|
#113692 |
| Guest.Visitor |
TechTip: JavaScript Made Easy, Part II—Is Caps Lock On?
Oct 28 2007 12:49:00 I am using Firefox ver 2.0.0.8 and it works fine for me. This isn't really about the caps lock key, it's the SHIFT key - detecting if that (SHIFT) is pressed or not and if the typed characters are upper case or not. If no shift key is pressed and letters are upper case, the caps lock must be on. Of course, this won't work if someone pastes into the field. <p>Chris
|
#113691 |
| Guest.Visitor |
TechTip: JavaScript Made Easy, Part II—Is Caps Lock On?
Oct 28 2007 04:17:00 Hi, <p>As I see it this is one of these things that happens when creating web solutions - although there are specifications layed out there will always be differences in the way browsers handles things and this seems to be one of them. I have not found a soloution to the Firefox problem but I did find a very interesting page about the subject. Have a look at: <p><a href="http://www.quirksmode.org/js/keys.html">http://www.quirksmode.org/js/keys.html</a> <p>Best regards <BR>
- Jan |
#113690 |
| Guest.Visitor |
TechTip: JavaScript Made Easy, Part II—Is Caps Lock On?
Oct 25 2007 03:25:00 Works well in IE...but not in FireFox 2.0 <p>well, it detects if the characters are in uppercase, but not if the caps key is pressed. <p>If you hold down shift and type, it treats that as if caps lock is on. <p>Obviously if the user is holding shift, they want capitals. <p>sorry... :(
|
#113689 |
| MC Press Web Site Staff |
TechTip: JavaScript Made Easy, Part II—Is Caps Lock On?
Oct 28 2007 21:19:00 This is a discussion about <B>TechTip: JavaScript Made Easy, Part II—Is Caps Lock On?</b>.<p align='center'><a href=http://www.mcpressonline.com/mc?1@232.1KNKfHX1eQT.17@.6b50a716>Click here for the article</a>.</p>
|
#113688 |




