Solved

Java applet calling Javascript Functions..

Posted on 1998-06-26
6
188 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 59
groupNoAdj 7 85
backtracking recursion  code 19 53
JList custom Cell Renderer refresh 15 53
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now