Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

More frequently sought solutions

Our esteemed columnist tackles 10 more of your most common JavaScript questions

  • Print
  • Feedback

Last November, the JavaScript column in this illustrious publication presented a series of "frequently sought solutions" -- 10 of the most commonly asked-for JavaScript code solutions. The column struck a nerve and you asked for more. I can't turn away such a worthy bunch, so this month, we'll tackle 10 more frequently asked questions. Specifically, we'll find out how to:



  • Pass a nonstandard number of parameters to one function
  • Play a script when starting Netscape
  • Link to one page for JavaScript browsers and another page for other browsers
  • Process a link before loading a new document
  • Display a link for JavaScript users only
  • Use string methods with the window.location object
  • Display a clock on a page
  • Display the document source/document info windows
  • Link to a different page if the user's browser does not support JavaScript
  • Wait for a period of time before executing JavaScript code


That's a lot to cover, so let's get going. SUBHEAD: Pass a nonstandard number of parameters to one function Functions are typically "formally" declared to accept a certain number of parameters, and standard programming practice dictates that you always call a function with the right number of parameters. For example, this script uses a showMessage function that is formally declared to accept two parameters -- Message1 and Message2 -- which are used as messages in an alert box:



<![if !supportEmptyParas]> <![endif]>
showMessage ("Hello", "There");
<![if !supportEmptyParas]> <![endif]>
function showMessage (Message1, Message2) {
      alert (Message1);
      alert (Message2);
}


But JavaScript is more flexible than this. You can define a function with no formal list of parameters and call it using any number of parameters -- or no parameters, if you wish. JavaScript supports a special arguments object -- an array -- that is created inside a function. In addition to a length array that specifies the number of arguments passed to the function, each element of the array is a separate parameter passed to the function from the original "caller." For example, if you pass the revised showMessage function two parameters, the arguments object contains these values:



<![if !supportEmptyParas]> <![endif]>
arguments.length = 2
arguments[0] = "Hello"
arguments[1] = "There"


Code inside your function can determine how many arguments to process, and then go through the arguments array, one element at a time, and pick out the arguments that were passed to the function. Here's the revised version of the showMessage function, which is able to accept any number of parameters:



<![if !supportEmptyParas]> <![endif]>
showMessage ("Hello", "There");
showMessage ("Hello", "There", "Again!");
<![if !supportEmptyParas]> <![endif]>
function showMessage () {
      for (var Count = 0; Count < showMessage.arguments.length; Count++) {
              alert (showMessage.arguments[Count]);
      }
}


JavaScript also supports a caller property of the arguments object. This property contains the object string of the function that called this function. This means that you can use the caller property to determine the name of the "calling" function, and then use that name to determine what course of action to take. You might have your function do one thing if X function calls it, and do something else if Y function calls it. Here's a short demonstration of the caller property, along with some code to extract just the name of the calling function. This name is displayed in an alert box:

  • Print
  • Feedback

Resources