DaisyFieldWeb Tools

ScriptCradle Help

User Manual for DaisyField ScriptCradle 

Contents:

ScriptCradle is an HTML document that you can use as a tool for learning the Javascript language, and as an aid in developing and debugging scripts written in that language. ScriptCradle itself is written in HTML and Javascript.  Your browser should be Internet Explorer 6 or Netscape Navigator 6, or a later version of these browsers.  ScriptCradle has not been tested with other browsers.

Starting ScriptCradle

Simply open the ScriptCradle page (scriptcradle.htm) in your browser.

Using ScriptCradle

The ScriptCradle document contains two TEXTAREA elements, which are rendered by your browser as two scrolling rectangular areas on the screen each capable of containing many lines of text. The left text area is the script area, and the right text area is the output area. To use ScriptCradle, you carry out these steps.

  1. Enter some Javascript code into the script area. You can include any Javascript statements including function definitions and function calls.
  2. Press the "Run Script" button.
  3. Did you get an error message (e.g., "A Runtime Error has occurred. Do you wish to debug?") from your browser?
  4. Yes? There is an error in your script. Click the NO button. You do not wish to debug! Go back and correct your script and press RUN again.
  5. No? Your script ran correctly. Try making some enhancements to your script and press RUN again.

Using the ScriptCradle output functions

The script inside scriptcradle.htm includes the definition of several functions that you can use to write text to the output area. These functions are dump, dumps, dumpl, and dumpo. The name consisting of a single dollar sign ($) has been assigned to be a synonym for dumpl. Thus you can use dumpl and $ interchangeably.

You can view the definitions of these functions by using the View Source menu command when you have ScriptCradle open in your browser.

Be sure you understand that dump, dumps, dumpl, and dumpo are not part of the Javascript language. They are available only while using ScriptCradle, since they are defined inside scriptcradle.htm. Furthermore, these functions have a purpose that is specific to ScriptCradle, namely, to write text to the "output" textarea control in the ScriptCradle window. All of these output functions accept a variable number of arguments. Here is a summary of what they do. (In the table, a1, a2, ... represent any Javascript expressions, and o1, o2, ... represent any Javascript expressions that evaluate to arrays or user defined objects.)

ScriptCradle Output Functions

Function

Description

Example

dump(a1,a2,...) Appends the string representation its arguments to the text in the output area. No whitespace is inserted after each item
var x='Something'
dump(x)
dump(x, 'Else')
dumps(a1,a2,...) Similar to dump except it inserts a space after each item
var y=100, u=[4,3,2]
dumps('Coke')
dumps('Pepsi')
dumps(y+17, 43,u)
dumps('Red','Green', 'Blue')
dumpl(a1,a2,...)
or $(a1,a2,...)
Similar to dump except it inserts a newline after each item
var y=100, u=[4,3,2]
dumpl(u)
dumpl("Red","Green","Blue")
$(y-100.4, u)
dumpo(o1, o2,...) Intended for dumping arrays or user defined objects to output. Dumps them in two columns, where the first column contains property names and the second contains property values.
var a =
new Array('Red', 'Green', 'Blue')
dumpo(a)

How does ScriptCradle work?

Look at the source code of scriptcradle.htm, and find the function runScript. A simplified version of this function is:

function runScript()
{
  // this function "runs" the script stored in
  // scriptTextArea.value
  outputString =''
  theScript = new Function(scriptTextArea.value)
  eval(theScript())
  outputTextArea.value = outputString
}

This simple code is the the heart of ScriptCradle. All it does is define a new function, theScript whose function body equals whatever the user typed into the text area. Then runScript uses the native Javascript eval() function to execute the function theScript that was just defined. Execution of the function theScript may have the side effect of causing something to be written to the global variable outputString if the user called the ScriptCradle output functions in the user's script. This string is then written to the output area.  (The actual code for runScript includes error trapping so we can inform you if your script contained errors.)

Example: Listing all primes less than 10000

Here is an example using ScriptCradle which will give you a feel for the speed and power of Javascript. Paste the following code into ScriptCradle's code area, and press RUN. ScriptCradle will fill the output window with a list of all prime numbers less than 10000.

function primes(n)
{  var mask=new Array(n+1), i, p
  for (i=1;i<=n ; i++)
    {mask[i]=true}
  for (p=2; p*p<=n; p++)
  {
    if(mask[p])
    {
      for(i=2*p;i<=n;i+=p)
        {mask[i]=false}
    }
  }
  for (i=0,p=2;p<=n;p++)
  {
    if(mask[p]){ mask[i++]=p }
  }
  return mask.slice(0,i)
}
var thePrimes=primes(10000) ;
var j ;
for(j=0; j<thePrimes.length; j++)
  dumps(thePrimes[j]);

See Also:

The DaisyField DOMpeeper is a tool similar in some ways to ScriptCradle, and has its own help file. DOMpeeper is designed to display complex objects such as those used in the w3.org DOM object model, and to allow rapid navigation of objects including drill down into object members.