Interaction between Applet and JavaScript

Source: Internet
Author: User
To use JavaScript objects in Java
The Netscape. Javascript package is defined as follows:
 
Netscape. Javascript. jsobject allows Java code to access Javascript methods and attributes;

Netscape. Javascript. jsexception allows Java code to handle JavaScript exceptions;
Netscape. plugin. plugin allows the client JavaScript and applets to operate on a plug-in (
Plug-in ).
From JavaScript 1.2, these classes are submitted to a file suffixed with. jar.
In the previous JavaScript version, these classes are submitted to a file suffixed with. Zip. To access
Liveconnect class. You can place the. jar or. ZIP file in the JDK Compiler's
In classpath:
 
Create a classpath environment variable to specify the path and name of these files;
When you use the-classpath command line parameter to compile, specify the location of these files.
Use jsobject to access Javascript 
Assume that you have a Java class named javadog, And the javadog constructor Sets
JavaScript Object jsdog, which is defined as the jsobject type, its code is as follows:
 
Import Netscape. Javascript .*;
Public class javadog {
Public String dogbreed;
Public String dogcolor;
Public String dogsex;
// Define the class Constructor
Public javadog (jsobject jsdog ){
// Use try... catch to handle jsexceptions here
This. dogbreed = (string) jsdog. getmember ("breed ");
This. dogcolor = (string) jsdog. getmember ("color ");
This. dogsex = (string) jsdog. getmember ("sex ");
}
}
 
Now let's take a look at how to use Java to access the client's JavaScript, but the HTML page must allow
Access JavaScript with a Java Applet-specified by the <APPLET> flag
MayscriptAttribute. You must obtain
The handle of the Navigator window, which can be used in the Netscape. Javascript. jsobject class
Getwindow method. For example, if a jsobject class variable is previously defined
Win, the following code assigns the window handle to win:
 
Public class myapplet extends applet {
Public void Init (){
Jsobject win = jsobject. getwindow (this );
}
}
 
Handle JavaScript exceptions in Java 
When a runtime error occurs in the JavaScript code called from Java, it sends
You can use the try... catch statement to catch the exception. For example:
 
Try {
Global. eval ("foo. bar = 999 ;");
} Catch (exception e ){
If (E instanceof jsexception ){
Jscodefailed ()";
} Else {
Othercodefailed ();
}
}
 
Access JavaScript objects and attributes 
We use the getmember method to access JavaScript objects and attributes, for example, the following code
Allow you to use the variable myform to access the JavaScript Object document. testform:
 
Public void Init (){
Win = jsobject. getwindow (this );
Myform = win. eval ("document. testform ")
}
 
You can also use the following statement to replace myform = win. eval ("document. testform "):
 
Jsobject Doc = (jsobject) win. getmember ("document ");
Jsobject myform = (jsobject) Doc. getmember ("testform ");
 
If the JavaScript Object document. testform. Jazz is a checkbox, the following statement
Allow you to access its checked attributes:
 
Public void Init (){
Win = jsobject. getwindow (this );
Jsobject Doc = (jsobject) win. getmember ("document ");
Jsobject myform = (jsobject) Doc. getmember ("testform ");
Jsobject check = (jsobject) myform. getmember ("Jazz ");
Boolean ischecked = (Boolean) Check. getmember ("checked ");
}
 
Access Javascript 
The method in the Netscape. Javascript. jsobject class allows you to find any
Javascript expression. First Use getwindow to obtain the handle of the Javascript window, and then use
Eval to access a JavaScript method, as follows:
 
Jsobject. getwindow (). eval ("expression ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.