|
|
Using JavaScript to Create a Powerful GUIby Meg Hourihan12/21/2001 The majority of computer users are familiar with the Windows graphical user interface (GUI). Most people know how buttons, toolbars, and tabs work, based on their experience with Microsoft Word or their email client. But contrast the consistent interfaces of client applications with online applications, and you find nearly every site does it differently. A user needs to learn how to use each Web application from scratch. While many applications aren't particularly complicated, it can still be a frustrating experience for a user to always feel like a newbie, needing to relearn an app interface again and again. By utilizing JavaScript and CSS to build standard client interface components in your online application or Web site, you can make it easier for your users to understand, at a single glance, what actions are available to them and how to accomplish their tasks. The result? A site or application with powerful functionality and users who feel more confident about their actions and are less likely to make mistakes. Now perhaps you're saying, "I didn't know JavaScript could do that!" Or perhaps you've seen a toolbar, or something similar on another site, but you don't know how it was accomplished. In this article, I will discuss how to build a simple formatting toolbar (like you see in Microsoft Word) to add functionality to any Web site that allows for user feedback or comments via a A Short DisclaimerThe code that follows uses the But don't let that stop you from implementing some of the ideas I'll discuss below. If many of your users are on IE4+/Windows, you can offer additional functionality today that can improve the look and experience of your Web site. (For assistance in determining what browser your visitors are using, see Danny Goodman's Object Detection article.) Building a Simple Toolbar
Let's start by creating a simple toolbar to hold three buttons: a bold, an italic, and a link button. This toolbar is a nice way to add functionality to an existing text area. It allows users to add simple formatting to their entry without needing to know HTML to do so. Any site that allows user participation or feedback, from Yahoo! Message Boards and community sites like Slashdot or MetaFilter to weblogs utilizing comment features, can be enhanced with this toolbar. Our toolbar can be broken into four areas of functionality:
Let's start by examining the two functions that will handle the insertion of HTML into our Using JavaScript to Manipulate Text Selections
format_sel() takes one parameter, a string, representing the tag value you wish to wrap around the selected text. In the case of the toolbar, we will use this function to wrap text in <b> and in <i>. If you wished, you could use <strong> and <em> in place of <b> and <i>. Or you could use this function to underline a selection (by using the deprecated <u>) or wrap the selected text in any tag of your choosing (<sub>, <H1>, etc.)
We can use the
Our second function
|
|
|
|
|
||||||||