Solved

Java applet calling Javascript Functions..

Posted on 1998-06-26
6
178 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now