Archive for July 2008
Unobtrusive Javascript: Self-labeling text inputs
Some web sites have self-labeling text input boxes; for example, see the text box at the top right of the page on memberhub. When these self-labeling form fields are empty, they contain helpful text that labels or further explains their purpose, such as “Search” or “Enter your favorite color.” As soon as you click on these fields, the help text vanishes and you can type in a value.
Using unobtrusive Javascript (see introduction), we can add behavior to a text input element to automatically label it with this sort of help text contained within the element. We will take advantage of the title attribute of input elements to do this. The title element is already widely used in many browsers to provide help text in a tool-tip when you hover over an element with your mouse, and we will steal this title text from any input text field to use for self-labeling purposes. Here is a script that accomplishes that:
autolabel.js
Event.onReady(function() { $$('input[type="text"][title]').each(function(inputElement) { var e = inputElement; var color = e.getStyle('color'); var fontStyle = e.getStyle('fontStyle'); if(e.value == e.title) { // FF reload behavior. e.value = ''; } var blank = !$F(e); var blurHandler = function(ev) { blank = !$F(e); if(blank) { e.setStyle({ 'color' : 'darkgray', 'fontStyle' : 'italic' }); e.value = e.title; } } e.observe('focus', function(ev) { if(blank) { if($F(e) == e.title) { e.value = ''; } e.setStyle({ 'color' : color, 'fontStyle' : fontStyle }); } }); e.observe('blur', blurHandler); blurHandler(null); Event.observe(e.form, 'submit', function(ev) { if(blank) { e.value = ''; } }); }); });
Here’s how to use it. Note that you need to include the Prototype and Low Pro Javascript libraries:
example.html
<script src="/js/prototype.js" type="text/javascript"></script> <script src="/js/lowpro.js" type="text/javascript"></script> <script src="/js/autolabel.js" type="text/javascript"></script> . . . <input type="text" name="color" title="Enter your favorite color" />
How it works
The script first searches for all input elements in the document that are of type “text” and also have the title attribute. For each of these, it executes a function to add the self-labeling behavior to the element. This function does a number of things:
- When you refresh a page in Firefox, as long as you don’t do a full reload, Firefox preserves whatever values were previously in form fields. The function tests if the current value of the form field is equal to the title attribute (meaning that someone refreshed the page while the self-labeling description was present in the field, and if so, the function clears the form field.
- For all other purposes, the function uses the variable blank to track whether the field is blank and should have the label inserted. We do this rather than comparing the field’s content to the title tag, in case the user actually types in the value of the title tag (for something like “Search”).
- The function adds a handler for the focus event (cursor enters field). If the field is blank, it clears the label text so the user can enter their own text, and restores the field’s color and font style to their original values. Otherwise, the field contains user text so it is left unchanged.
- The function adds a handler for the blur event (cursor leaves field). If the field is blank, it remembers the fact that it is blank, sets the style of the field so that the text is italic and dark gray (you may modify this as you wish), and then inserts the title text.
- The function adds a handler for the submit event on the field’s form. If the form is submitted and the field is blank, it will be cleared so that the correct value is submitted for the form contents.
Improving Firefox session restore
I use Firefox session restore regularly. This saves all my open tabs whenever Firefox closes (or crashes) and restores them when it reopens. Unfortunately, there are still cases where Firefox will forget my open tabs. This happens, for example, when I close my main window, thinking that I’m closing Firefox, but then I realize that there is still a pop-up window open. When I restart Firefox, that popup will be restored instead of my old tab list!
I have not yet found a way to recover the tabs. What a sinking feeling this leaves you with! My open tabs are sometimes the result of several weeks’ worth of browsing. I’ve grown better at saving links using del.icio.us rather than holding them in open tags, and obviously I need to work harder at this. But I often still have up to 20 tabs open at any given time, waiting to be read.
This happened to me again today. So I resolved one more time to try to find a solution. And I did! I found and installed Tab Mix Plus (I installed the latest TMP development build). This Firefox plugin remembers your sessions from more than just the most recently closed window. So if you close your main window first but find a popup lingering, you can close the popup without worrying that you have lost the tabs from your main window. What a relief!
Here are the settings that I chose in TMP:
- When you first enable Tab Mix Plus, it asks you whether you want to use the Firefox session restore feature. The TMP session restore is much better than Firefox’s, so choose No.
- Go to Tools|Tab Mix Plus Options to choose other options.
- Click the “Events” icon. On the “Tab Features” tab, un-check “Ctrl-Tab navigates tabs in the most recently used order.” This will set Ctrl+Tab to behave the way it usually does in Firefox.
- Click the “Session” icon.
- Make sure that “Use Firefox’s built-in Session Restore feature” is not checked.
- Under “When Browser Starts”, select “Restore”.
- Under “When Browser Exits”, make sure “Save Session” is selected.
Tab Mix Plus provides additional control over your browser sessions. Using the Tools|Session Manager menu, you can manually save sessions, and also restore older sessions.
Editor Color Scheme
Recently Slashdot featured a discussion of the best color scheme for programming. From that discussion I’ve discovered the zenburn color scheme, and have switched to it. I like the fact that the background is not stark black; the reduced contrast feels easier on my eyes.
Here are some resources I found from that discussion for color schemes:
- Compare hundreds of vim color schemes all on one page.
- Zenburn for vim.
- Some recommendations for tweaking zenburn (I am using the high contrast setting and also the 256-color setting).
- Zenburn for Emacs and for Visual SlickEdit (for the latter you’ll need to register for free before the download links will appear).
What color scheme do you prefer for programming? There are several others at the vim color scheme test, above, that I’m also interested in trying out.
See also: Programming Fonts