RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Implement a Browser Command Line with JavaScript : Page 4

AJAX techniques offer sophisticated GUIs on the web, but sometimes you need to execute commands at the command line. This article demonstrates how to build a custom command line you can add to your user interface—and even get a free run-time scripting engine.

Further Ideas
The code for this command line is very short—only about 100 lines. This is because it's pretty bare bones. Here are some ideas for making this code more useful.

Unix-Style Options
This command line syntax lets you use a sequence of words, such as "g i", to implement a specific command. Expressing this as "g -i" would be more Unix-like, so this might be a useful modification. You can implement this in a couple of ways:

  1. Strip the dashes from the options and call the g_i() function.
  2. Have a single function, g(), that is responsible for interpreting the options.

Function Disambiguation
I named the 'alert' command al(). If you wanted the command to be the full word 'alert', then you would have to implement a handler called alert(), but this isn't allowed. JavaScript already has a function called alert(), which you shouldn't try to redefine.

One solution to this problem is to use a prefix for handler names: bcl_g(), bcl_g_i(), and so on. This could be made mandatory, or it could be optional. If it's optional, the code would have to check for the existence of bcl_g(). If it didn't exist, the code could then check for g().

You can use the browser command line to call any JavaScript function, not just your specially defined handlers. A handler is just a regular function, after all. Thus, you can run the command alert hello, which will show an alert window displaying the word "hello." However, putting a script like this in a place where anyone can access it is asking for trouble.

The solution to the disambiguation problem is also a solution to this security issue: require handlers to start with "bcl_", and specify that only handlers can be called. Another solution is to enclose all the handler functions inside an array or object, and allow the system to look for handlers only in that array/object. Neither of these solutions are perfect, however, because browser bugs and other vulnerabilities may allow a hacker to create JavaScript code to define functions starting with "bcl_", or to modify the object containing your handlers.

Commands in Separate Files
If you like using the browser command line, you will probably accumulate many useful command handlers. If you keep all of them in the same file with the command line code, it could get messy. Keeping the handlers in separate files would be useful, perhaps organized by functionality. Loading JavaScript code can be tricky, though, because of asynchronous loading issues. So you may want to consider using AJAX techniques or even an AJAX framework for loading code.

Better Parsing
The parser is very simple, separating the command into words by breaking it at whitespace. But consider the following command:

% al "hello world!"

This doesn't work right. It should display an alert box with the words "hello world!", but in fact it displays only the world "hello" (see Figure 4). The parser breaks the command up into the following words:


Click to enlarge

Figure 4. Parser Breaks Up the "hello world!" Command

Although both arguments are passed to al(), the al() function recognizes only the first argument, so that's what it displays.

This problem can be solved using a more sophisticated parser—one that recognizes quotation marks. But this in turn requires a syntax for escaping quotation marks. A full command line parser has many elements—feel free to add what you need.

A Scripting Language for a Scripting Language
Command lines are useful for a number of reasons. It's much easier to run new code by calling it from a handler than it is to create a GUI to call it. A command line is a great way to try out new code before you go to the trouble of implementing a real interface. Command lines are also useful for impatient people, who don't like to use the mouse any more than necessary.

This command line implementation is also pedagogically useful: it demonstrates some basic DHTML/JavaScript techniques, as well as a JavaScript program's ability to inspect its own code and call functions dynamically.

The browser command line is, in a sense, a scripting language for a scripting language.

Greg Travis is a Java programmer and technology writer, living in New York City. After spending three years in the world of high-end PC games, he joined EarthWeb, where he developed new technologies with the then-new Java programming language. Since 1997, he has been a consultant in a variety of Web technologies.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date