Morphic Molecules Diamond

Morphic Molecules<
Dynamic Programming with Javascript

This page © Mona M. Everett, Ph.D., 1996

everett@txdirect.net


One of the most powerful features which a language can have is the ability to write and execute code. Up until the advent of Javascript, the only language which could do that was Hypertalk, the scripting language of Apple's Hypercard. Hypercard long preceded Visual Basic as an object oriented language with a drag and drop user interface. It can even act as a simple database with its metaphore of a stack of filing cards. Alas, it was written for the old black and white Macintosh and never really updated to a color program although Apple has added some color via an XCMD. Its color is limited and klutsy, however. Consequently, a really powerful program goes largely ignored and is totally unknow to Windows users.

Hypercard can modify its own scripts and write scripts on the fly for components which it can manufacture on the fly. It can also 'do' a field; e.g., if the field contains hypertalk, it can execute the code in the field. Although Javascript does not yet have the depth of Hypercard, it is catching up fast and has some unique features of its own. Of course, it supports as much color as your graphics card and screen can handle and has the added benefit of being available to all of the platforms for which Netscape makes a browser.

Although Javascript cannot alter its own script, or much of anything in its own window, it is perfectly capable of producing a new window or writing elements to the current page upon a rewrite of that page. It can also easily write to documents in other frames. The Morphics Molecules Pages contain examples of some of these. It is the aim of this page to show you how Javascript can write and execute code.


Sending an Alert



Enter the message for the alert box here.

Do NOT use any carriage returns in this field!
You can place some text for your alert box in the form at the left. When you are satisfied, click on the image button .
Go A javascript statement to open the window will be generated and placed in to the box below. You can execute the statement with the subjacent button.


After you write the Javascript into this field with the GO button, click here to see the window. If you get an error, check for carriage returns within the lines of the script.
Function pop1() is called by the go button and writes the Javascript string into the textarea whose value the doit1() function will "evaluate". If you want this transparent to the user, you could simply store it in a string and evaluate the string.
function pop1()
{
  var theForm = document.forms[0]                    //shortcut
  var astr = theForm.alertTxt.value                  //get value in textarea
  theForm.doAlertTxt.value = 'alert("' + astr + '")' //write HTML string; put in
                                                     //second text area
}


Function doIt1() actually evaluates the value of the textarea containing the string which you have just generated. Like Hypercard, as part of the evaluation, it actually carries out any Javascript it finds there. Unlike Hypercard, you have to point the eval function at the VALUE of the field (textarea). In Hypercard, you would use the statement, "do card field doAlertTxt". function doIt1() { eval(document.forms[0].doAlertTxt.value) }



Opening a New Window

Width Height
Scroll bars
Sizable
Toolbar
Location
Directories
Copy History
Status Bar
Enter text for your window here

Do NOT use any carriage returns in this field!
You can place some text and set some of the attributes of the window to be created in the form at the left. When you are satisfied, click on the image button .
Go A javascript statement to open the window will be generated and placed into the box in the frame below. You can execute the statement with the subjacent button.

After you write the Javascript into this field with the GO button, click here to see the window. If you get an error, check for carriage returns within the lines of the script.
Function pop2() is called by the go button and writes the Javascript string into the textarea whose value the doit1() function will "evaluate". Although you could write this to a long string and evaluate it, for debugging purposes it might be better to write it to a a text area. Be careful because some carriage returns in the middle of lines can be inserted and they will cause an error.
var aPopUp                                       //declare the window as global
function pop2()
{
  var theForm = document.forms[1]                //define theForm as shortcut
  var xx  = document.forms[1].stylchk            //stylchk is a checkbox array
                                                 //define it as xx
  var k = xx.length                              //get length of stylchk array
  var feature                                    //declare working variables
  var toggle
  var astr = "aPopUp=window.open('','Note','"    //Set up first part of the 
                                                 //statement which will open 
                                                 //the window.
  for ( var i = 0 ; i < k ; i++)                 //for loop will write all of 
                                                 //style variables.
  {
     feature = xx[ i ].value                     //value of checkbox is feature
                                                 //name
     toggle='NO'                                 //feature defaults to off
     if (xx[ i ].checked ) toggle = 'YES'        //but if checked turn it on
     astr += feature + '=' + toggle              //add to "open" statement
     astr += ','
  }
  astr += 'width=' + theForm.winwid.value + ','  //add width and height to 
  astr += 'height=' + theForm.winht.value        //"open" statement
  astr += "')\r\n "                              //finish up "open statement.
  astr += 'aPopUp.creator = self'                //set this up so that the
                                                 //child window knows its parent
  var mstr = theForm.theText.value               //get window text and write it
  astr += 'aPopUp.document.write("' + mstr + '") \r\n'  //to the popup's doc.
  astr += 'aPopUp.document.close()\r\n'          //close popup's doc
  theForm.doTxt.value = astr                     //script is done.  Put it into
                                                 //textarea that will be 
                                                 //"evaluated"
  alert(astr)                                    //optional check
}

Function doIt2() evaluates the doTxt text area and consequently executes any Javascript therein. function doIt2() { eval(document.forms[1].doTxt.value) }






This page was last updated on

Mail comments and corrections to

everett@txdirect.net

HREF="mailto:everett@txdirect.net">everett@txdirect.net