?
Solved

Java applet calling Javascript Functions..

Posted on 1998-06-26
6
Medium Priority
?
222 Views
Last Modified: 2010-04-16
I just found out that they changed the Security Issue for Javascript.. it will violate the rule if access from different domain..
so my problem lies that my Applet needs to set some values on some Html fields through javascript...... How can I get around this?   I am sorry about the points but that's what I have now :(
0
Comment
Question by:Sieger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 1223842
A very simple example of an applat that will call a javascript function named setVar in the document.

==================

import java.applet.*;
import netscape.javascript.JSObject;

public class JSObjectTest extends Applet {
      JSObject mozilla;
      String[] args;
      String msg;

      public void init() {
            msg = getParameter("msg");
            try {
                  mozilla = JSObject.getWindow(this);
            }
            catch (NoClassDefFoundError e) {}

            String args[] = {msg + ""};
            try {
                  mozilla.call("setVar", args);            
            }
            catch (NoClassDefFoundError e) {}
      }

}

===================

<HTML>
<HEAD>
<title>JSObjectTest.java</title>

<script language="JavaScript">
<!--
function setVar(msg){
      alert(msg);
}

// -->
</script>




</HEAD>
<BODY BGCOLOR="#FFFFFF">

<applet name="jsobjecttest" code="JSObjectTest.class" width=2 height=2 MAYSCRIPT>
<param name="msg" value="JSObject works">
</applet>

</body>
</html>

0
 
LVL 1

Author Comment

by:Sieger
ID: 1223843
sybe, I know this will work too.. but I need to know if there's anyway you can assign a text field in HTML to something in Java through Javascript.  So the code with sit in Java not javascript...  I need to know this because I made few applets like that before I found out the security issues and I want to know if I have to bring the code to HTML or I can do something in java which allow me to access javascript..
0
 
LVL 4

Expert Comment

by:evijay
ID: 1223844
Here is the sample

consider a piece of html code

<form name=myform action="http://somecgilink" method=post>
<input type=text name=hobbies>
</form>

The java code which sets the text area hobbies is as follows



JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject myform = (JSObject) doc.getMember("myform");
JSObject  text = (JSObject) myform.getMember("hobbies");
text.setMember("value", "BaseBall, movies, television");  // set the html form textfield using java






0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

by:Sieger
ID: 1223845
doesn't help.. same error.. but I found out the way to do.. now how can I delete this question...
0
 
LVL 1

Author Comment

by:Sieger
ID: 1223846
Sorry evijay.. it worked.. I didn't look at the code carefully..

can you propose the answer again so I can give you points..
and do you know if this will work for select list?
0
 
LVL 4

Accepted Solution

by:
evijay earned 120 total points
ID: 1223847
Here is the sample
consider a piece of html code
<form name=myform action="http://somecgilink" method=post>
<input type=text name=hobbies>
</form>
The java code which sets the text area hobbies is as follows
JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject myform = (JSObject) doc.getMember("myform");
JSObject  text = (JSObject) myform.getMember("hobbies");
text.setMember("value", "BaseBall, movies, television");  // set the
html form textfield using java

Yes. You can select a list item in java.
Consider the below example
<form name=myform action="http://somecgilink" method=post>
<SELECT NAME="accessories">
<OPTION name="aircond"> Air Conditioning </option>
<OPTION name="cdplayer"> CD Player </option>
</Select>
</form>

The steps to select option Air conditioning is
JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject myform = (JSObject) doc.getMember("myform");
JSObject  list = (JSObject) myform.getMember("accessories");
list.setMember("selectedIndex", 1);

Vijay


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question