advertisement -- please support sponsors

dynamic content, forms, programming:
"command-line" arguments

What are command-line arguments?

The term command-line argument can be informally defined as any parameter that is typed after the name of a program in a command interpreter (such as a DOS box or Unix shell). For example, if you typed the command

dir *.txt
in a DOS box, then 'dir' would be the name of the program to run and '*.txt' would be the command-line argument.

Web pages are programs.

If you stretch your imagination a bit, you can think of a web page as a program. The capabilities of web page "programs," of course, are limited mostly to displaying text in your browser -- but they are still programs none the less. Pushing this analogy further, it is not unreasonable to think of your web browser's location bar a 'command interpreter,' because it is here that you enter the name of the program (i.e., the URL) you wish to run (display). When you push enter in the location bar, the browser interprets the request you have typed and performs the appropriate action.

So, if web pages are programs, does it make sense to talk about command-line arguments for web pages? Actually, it does. The next time you fill out a web form that uses the GET method (such as Yahoo!TM's search engine), take a look at the URL shown in the location bar. There, you will see that the name of the program (i.e., the web page address) is followed by a question mark and then by a whole bunch of extra, URL-encoded characters. A URL-encoded string that appears after the question mark in a URL is called a search string. Search strings can be thought of as command-line parameters for web pages.

JavaScript programs can access search strings.

Most useful programming languages provide mechanisms for accessing command-line parameters. Although JavaScript is no exception to this rule, JavaScript's support for command-line arguments is at best very difficult to understand and use.

To make things easier, I have developed a script, called args.js, that makes accessing search string parameters a breeze. It is given in the edit box below in case you want to cut and paste it into your favorite text editor.

To use the script, save it in a file called 'args.js', and then activate it in your web pages using the following "include tags":

<script src="args.js"></script>

Note: If you have a question about what these tags mean, please see article #12, Importing Scripts From Other Files."

The script in args.js generates a text-associative array, called args [], from which you can read the URL's search parameters. For args.js to work, the search string parameters must be appended to the URL in the form of

http://www.mysite.com/mypage.html?param1=value1&param2=value2&...&paramn=valuen .
All of the parameters and values should be URL-encoded. (Don't worry! This is the default behavior of most browsers.)

For example, if the URL used to load your web page is

http://www.mysite.com/mypage.html?name=Charlie+Brown&password=snoopy ,
and args.js is included in the source for mypage.html, then the expression
args ['name']
will return the string 'Charlie Brown', and the expression
args ['password']
will return the string 'snoopy'.

On the other hand, if you try to access an element of args [] that doesn't exist, such as args ['telephone'], then the result will be a special JavaScript value called undefined, because no "telephone" parameter appears in the URL's search string.

Tip: For more information about JavaScript's undefined value, please see article #45, undefined vs. null.

a simple example

I have prepared a simple product ordering script to demonstrate the use of args []. When you click on the button below, a new window will appear showing blanks for "item," "price," and "quantity." Fill the blanks in with anything you want and then push the submit button (don't worry, it's just pretend :-). As you experiment with different numbers to change the results, pay special attention to the location bar and watch how it changes each time you enter new data.

Don't forget to study the source so you can see how it works.

Charlton Rose
June 11, 1997