Solved

Java applet calling Javascript Functions..

Posted on 1998-06-26
6
219 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 30 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

691 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