Using JavaScript Interface
QuestAgent provides an application programmer’s interface (API) for connection with Java Script methods built in your HTML pages. This API is compatible with both Microsoft Internet Explorer and Netscape Navigator. Using the "hidden" applet technique and JavaScript you can run queries, get results, set collections, and so on without using any of the standard applet visual components.
To use the JavaScript implementation, you must be familiar with JavaScript development.
In general, JavaScript has access to all of the applet’s public methods. JavaScript uses the object ID of the Java applet when accessing these public methods.
Note: "Search by Field" Applet (Applet FieldedSearchApplet.class) does not support JavaScript interface methods. However, SearchApplet.class can be used for search by more than one index field.
QuestAgent API Methods
The QuestAgent API contains methods for programmatically starting a search, getting hit information, loading a collection, and getting the last applet message:
startSearch
int startSearch( String query );
Performs a search for a specified query and returns the number of documents (hits) found or -1 if error occurs. For example: startSearch( "hotel or restaurant" )
If your index database has more than one index field defined, you will have to specify field in the query in format "field_name=keyword". For example: "title=xml and author=johns".
getHitTitle
String getHitTitle( int index );
Returns the document description of a specified hit index.
getHitLocation
String getHitLocation( int index );
Returns the document location of a specified hit index.
getHitField
String getHitField( int index, String fieldName );
Returns the content of record field for a specified hit index. Field name must be one of names defined by "store as" parameter in data mappings settings.
loadCollection
void loadCollection( int colInd );
Loads the index for a specified document collection.
getMessage
String getMessage();
Returns the last applet message.
showLocation
void showLocation( String url, String target );
Shows the selected document in a specified browser frame.
JavaScript Examples
The following example shows using JavaScript and the QuestAgent API (taken from qascript.htm). In this example, the query text box appears in one frame and the result list appears in the other frame. This is how QuestManager deploys the JavaScript implementation.
Notice that in the second listing, the applet’s width and height are each set to 0, which loads the applet but makes it invisible to the user.
Listing 1
The startup file (frameset.htm) contains the frame definition:
<HTML>
<FRAMESET ROWS="85,*" BORDER=0>
<FRAME NAME="qawin" SRC="qascript.htm">
<FRAME NAME="customwin" SRC="tips.htm">
</FRAMESET>
</HTML>
Listing 2
The JavaScript implementation is included in qascript.htm. Results are returned in the frame named customwin.
<HTML>
<SCRIPT LANGUAGE="JavaScript">
var multi = false;
function PerformSearch( qform )
{
// load active collection
if( multi ) {
loadCollection( qform );
}
// start search...
var res = document.questagent.startSearch( qform.query.value );
var hits = new Array;
parent.customwin.document.open();
// check result
if( res < 0 ) {
parent.customwin.document.write( "<h3>Error.</h3>" );
parent.customwin.document.write( document.questagent.getMessage() );
}
else if( res == 0 ) {
parent.customwin.document.write( "<h3>Nothing found.</h3>" );
}
else {
parent.customwin.document.write( "<h3>Search result:</h3>" );
parent.customwin.document.write( "<ul>" );
// print results
for( i = 0; i < res; i++ ) {
// print location and title
parent.customwin.document.write( "<a href=\"",
document.questagent.getHitLocation( i ),
"\">",
document.questagent.getHitTitle( i ),
"</a><br>" );
// print description if available
var desc = document.questagent.getHitField( i, "description" );
if( desc != null && desc.length() > 0 )
parent.customwin.document.write( "<ul>", desc, "</ul>" );
}
parent.customwin.document.write( "</ul>" );
}
// print footer
parent.customwin.document.write( "<font size=1>" );
parent.customwin.document.write( "Search technology by JObjects" );
parent.customwin.document.write( "</font>" );
parent.customwin.document.close();
}
function LoadCollection( qform )
{
var i = qform.coll.selectedIndex + 1;
document.questagent.loadCollection( i );
}
</SCRIPT>
<BODY>
<table border="0" width="100%">
<tr>
<td valign=top>
<font size="-1">
You can use this space for menus, titles, logos, images or
whatever you like. Search results will be shown in the
frame below.
</font>
</td>
<td valign=top>
<div align="RIGHT">
<FORM name="simpleform" onSubmit="PerformSearch( this ); return false;">
<INPUT TYPE="TEXT" NAME="query" SIZE="15">
<INPUT TYPE="BUTTON" onClick="PerformSearch( this.form )" VALUE="Find">
<br><font size="-1">
<a href="tips.htm" target="customwin">Search Tips</a>
</font>
</FORM>
</div>
</td>
</tr>
</table>
<APPLET
CODE = "com.jobjects.quest.SearchApplet.class"
ARCHIVE = "qagent.zip"
NAME = "questagent"
ID = "questagent"
WIDTH = 0
HEIGHT = 0
MAYSCRIPT
>
<PARAM NAME="CABBASE" VALUE="qagent.cab">
<PARAM NAME="ParamFile" VALUE="qagent.prm">
</APPLET>
</BODY>
</HTML>