Tabs in TEXTAREA
I was working on a small personal project the other day and I ran into a surprising obstacle. I wanted an online form to behave just like notepad does. Specifically I wanted to use tabs in a textarea of a form. The shocking part was how scarce the information on the issue was and how bizarre some approaches were. I ended up writing my own JavaScript to handle this issue and realized that my code works so well that I need to put it out there for others to use. You can check it out in action HERE.
Tested with: IE6, IE7, IE8, FireFox, Chrome, Safari, and Opera
Here is the business end that goes into the "HEAD" section.
<script type="text/javascript">
// This script allows intuitive TAB functionality in HTML form fields.
// Due to browser incompatibilities the following code has to be included in the field tags:
// onkeydown="checkTab(event)"; onkeypress="operaTab(event)";
// visit MattStypa.com for more information
function checkTab(event)
{
// First we check if the key pressed was a tab
if (event.keyCode == 9)
{
// Is this IE?
if (navigator.appName == 'Microsoft Internet Explorer')
{
// This stops the default behavior of the tab key. preventDefault does not work in IE.
event.returnValue = false;
// This gets the current selection or cursor location
range = document.selection.createRange();
// Replaces the current selection with a tab character or simply inserts it at the current cursor position
range.text = "\t";
// This resets the selection
range.collapse(true);
// And this executes above statement.
range.select();
}
else
{
// This stops the default behavior of the tab key
event.preventDefault();
// We need to save the current position of the cursor.
var c = event.target.selectionStart;
// Inserting a tab into the string.
// First we grab what comes before the current selection (or cursor position).
// Then we add the tab character. Finally we add the part the comes after the
// current selection (or cursor position).
event.target.value = event.target.value.slice(0,event.target.selectionStart).concat("\t").concat(event.target.value.slice(event.target.selectionEnd,event.target.value.length));
// This sets the cursor position.
event.target.selectionStart = c + 1;
// And this cancels selection. Opera requires that this is on a seperate line.
event.target.selectionEnd = c + 1;
}
}
}
function operaTab(event)
{
if (navigator.appName == 'Opera' && event.keyCode == 9)
{
// This stops the default behavior of the tab key
// Opera is unable to do that when called from onkeydown
event.preventDefault();
}
}
</script>
Also we need to map some events to the field.
<textarea onkeydown="checkTab(event)"; onkeypress="operaTab(event)";></textarea>
Switching to Chrome
I have been using Firefox since its first release back in 2004. It is an amazing browser. It's fast, it's secure, it has tons of add-ons and most of all it's open source. However, for some time now, I have been considering switching to Google Chrome. This browser has been shown to perform better then Firfox in numerous speed and compliance tests.
One of such tests is called ACID3, and it checks for browsers compliance with HTML, CSS, JavaScript etc. It's hard to believe that there are only 3 browsers for windows that pass this test, and it's beyond me how the newest, not even released yet, Internet Explorer 9 scored 55/100 while iPhone scored 100/100.
Compliance and Speed aside, I think that the most important feature of a browser is it's security. Fortunately there is a test for that as well, and it's called Pwn2Own. Actually it is a competition where experts try to compromise each browser. This year only Chrome remained un-hacked the first day. Of course it does mean that it's 100% secure, it means that it is harder to brake then the rest. With that in mind I would strongly recommended that you consider switching as well.
WordPress
It seems that writing about an amazing blogging system is quite appropriate for a first post on my, well, blog. The one I chose is called WordPress. At first I was skeptical about it and a visit to WordPress.com did not convince me to try it. I thought that it is just another hosted service such as Blogger. My good friend enlightened me and pointed to WordPress.org where I learned what it truly is. Essentially, WordPress is an Open-Source PHP script that, together with MySQL, provides every feature a blogger might desire.
I fell in love with this system right away. This truly is what I have been looking for. And I am not the only one to think that. WordPress is used by more then 200 million websites worldwide and is consistently gaining more supporters. You may ask, "What makes it so much better then anything else out there?" Lets take a look.
Besides the standard features you would find in other blogging system, WordPress has support for Themes and Plugins that expend its looks and functionality. One of the cool things about those is that they can be installed directly from the control panel. You dont need to upload any files through ftp anymore. On top of that WordPress is so flexible that it is also commonly used as CMS or even a photo gallery.
Multiple tags and categories can be assigned to your posts. Those in return are displayed in an appropriate widget and help visitors find related or other interesting topics on your blog. Widgets are like building blocks that can be placed, organized, and customized to fit your own needs. Some of these include Archives, Links, Pages, and even a Tag Cloud.
WordPress also features an amazing Media Library that allows you to easily upload your images. It even has a simple image editor build right in. This whole thing is simply stunning.
P.S: Welcom to my Blog