Not so long ago I got a couple of e-mails. One asked if I knew how to control an Applet using JavaScript and the other asked me if I knew how to call JavaScript routines from an Applet. At the time I didn't have a clue. However, not being one to turn down a challenge I managed to solve the first problem and this document describes how I solved the second.
Having little experience of Applets I had to ask myself: "CAN it be done?". Well, having looked behind the scenes of the Action Buttons in the R5 View Applets, I discovered they do nothing more than call JavaScript function similar to their HTML counterparts, I knew it could be done. So I set off to find out how...
By way of example here is an Applet that lets you select a colour or add a new one and then, using the "Add" button, pass the value to a JavaScript function in the document. This function then appends the value to the <textarea> called "Choices".
You have chosen all of these:
This particular example is by no means rocket-science and, at the end of the day, is next to useless. However, just imagine the possibilities of combining Java AND JavaScript on your page. One thing that jumps to mind (well mine anyway) is that the Applet could use CORBA to connect to the server. This means we could fetch data from the server without having to reload the page. Nice! No more Keyword Refresh nightmares.
So how do we go about writing an Applet like this. Well I'm not about to start talking about how to write an Applet, just the bits relevant to this example. The technology that is used was created by Netscape (wow, they have done something worthy of praise) and is called LiveConnect. This is the same technology used when JavaScript talks TO the Applet.
The first thing to do is make sure that the necessary classes are imported to the applet using the line below:
import netscape.javascript.*;Now we can use this in the code-block below:
public void doAdd() {The first thing we do here is create a JSObject object that represents the browser window object. The method then looks at the drop-down list and the text field on the applet and works out what the user wants to send to the browser. This is then passed to the browser window, via JavaScript, using the eval method.
//Get a handle on the DOM Window Object of the browser;
JSObject win = JSObject.getWindow(this);
//Work out what to send.
String add = null;
String choice = getdpdChoose().getSelectedItem();
String addnew = gettxtAddNew().getText();
if ( !choice.equals( getParameter( "DefaultChoice" ) ) ){
add = choice;
} else if ( !addnew.equals("") ) {
add = addnew;
} else {
add = "";
}
//Send the value to the JavaScript function in the browser
win.eval("addToList(\'" + add + "\')");
}
function addToList( val ){As I mentioned earlier this methodolgy is VERY powerful. There are almost endless number of applications. If you come up with anything interesting, don't forget to share it with us all...
if ( val == "" ) return alert( 'You haven\'t selected
anything in the applet.' );
fld = document.forms[0].Choices;
fld.value += (fld.value=="") ? val : '\n' + val;
}
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net