★ wanayoo — archive 1999 http://developer.netscape.com/docs/examples/livewire/liveconnect_js_java.htmlNouvelle recherche | Portail wanayoo



USING LIVECONNECT TO ACCESS JAVASCRIPT FROM JAVA

Sometimes is easier to manipulate Netscape Navigator properties in JavaScript than directly from a Java applet. However, Java alone does not provide you with the ability to do this. Now, with LiveConnect, you can access JavaScript functionality from inside Java. The following example demonstrates setting and getting a cookie with a JavaScript function call from a Java applet.

<html>

<head>
<title>Testing</title>
<script>

function setCookie(txt){
    document.cookie = txt;
    alert ("String before setting cookie : " + txt);
}

function getCookie(){
    var cookieAfter = document.cookie;
    alert ("String after setting the cookie : " + cookieAfter);

    return cookieAfter;
}

</script>
</head>

EXAMPLE

Example of using the Java call() method to call a JavaScript function.

Enter some text and click the SetCookie button. The JavaScript button handler will pass the text to setString() method of the SGCookie applet. The applet creates a local copy of the string and calls the Javascript setCookie() function to set the text into the cookie. The applet calls the Javascript getCookie() function to retrieve the cookie text. The SGCookie Applet prints the the cookie text to the Java console.

<form name=myForm>
<input Type=text value="Cookie text" name=cookieTxt>
<input Type=button value="Set Cookie"
onClick="document.cookieApp.setString(document.myForm.cookieTxt.value)">
</form>

<!-- The MAYSCRIPT attribute is required in order for the Java methods to be available to the JavaScript functions. -->
<applet code="SGCookie.class" name=cookieApp MAYSCRIPT>
</applet>

</html>

Here is the Java code for SGCookie.class. To compile this class it may be necessary to unzip the java_30 file included with the Navigator. Place the contents of the unzipped java library in a directory specified on your classpath. Don't do this in your Navigator java classes directory or you will run into versioning problems the next time you update to a new version of Navigator. Make sure the Netscape classes are the first, and only classes on the classpath at compile time.

import java.awt.*;
import java.lang.*;
import java.applet.Applet;
import netscape.javascript.JSObject;

public class SGCookie extends Applet {

    JSObject win;
    
    public void init(){

        // Get a reference to the Navigator Window
        try{
            win = JSObject.getWindow(this);
            }catch (Exception e){
                // Don't throw exception information away, print it.
                e.printStackTrace();
                }
        }   
    
    public void setString(String cookieTxt){


        String msg[];
        msg = new String[1];

        // Create a local copy of the cookie text
        msg[0] = cookieTxt;
        
        // Call the JavaScript function to set the cookie             
        win.call("setCookie", msg);
        
        // Call the other JavaScript function to get the cookie
        String txt = (String)win.eval("getCookie()");
        System.out.println(txt);
        }
}

For the latest technical information on Sun-Netscape Alliance products, go to: http://developer.iplanet.com

For more Internet development resources, try Netscape TechSearch.


Copyright © 1999 Netscape Communications Corporation.
This site powered by: Netscape Enterprise Server and Netscape Compass Server.