Solved

Java applet calling Javascript Functions..

Posted on 1998-06-26
6
213 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
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!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 53
HashTable highest marks enumeration alternative 9 48
Why my table column Id is not passed to java object? 4 46
Running JavaFX on the Raspberry Pi 27 150
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

730 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