hi
program is not giving the alert message in internet explorer.I am using the first example give in the your link .
thanks
gaurav
Main Topics
Browse All TopicsHi
Suppose this is my java program.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class testscript extends Applet
{
Button btest;
public void init()
{
try
{
btest=new Button("Test HjavaScript");
}
catch( Exception e){
System.out.println(e);
}
}
public void start()
{
}
public boolean handleEvent(Event e) {
if((e.id==Event.ACTION_EVE
{
String msg=new String("Hi Gaurav");
}
}
}
this is my java script
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript -
function MsgBox (textstring) {
alert (textstring) }
// - End of JavaScript - -->
</SCRIPT>
</HEAD>
</html>
How I will call java script function MsgBox from java applet program.I call this function on button click of the java program.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
as i said you,i used the first example given in your link
////java code
import java.applet.*;
import java.net.*;
public class inJava extends Applet{
public void init(){
String msg = "We will jump to an HTML tag";
try {
getAppletContext().showDoc
(new URL("javascript:doAlert(\"
getAppletContext().showDoc
(new URL("javascript:jumpTo(\"#
}
catch (MalformedURLException me) { }
}
}
//html page
HTML><HEAD></HEAD><BODY>
<SCRIPT>
function doAlert(s) {
alert(s);
}
function jumpTo(tag) {
self.location=tag;
}
</SCRIPT>
<APPLET CODE="inJava.class"
NAME="myApplet" MAYSCRIPT
HEIGHT=400 WIDTH=10>
</APPLET>
<P>
<A NAME="JUMP">jump here from Java via Javascript</A><br>
</BODY>
</HTML>
Netscape AND Microsoft
thanks
hi
A java script function return type is String.how I handle that function in java apllet.
I try with this.
in java
line=getAppletContext().sh
here line is of type String.in javascript it is like this.
function getText()
{
return document.getElementById("U
}
But it is giving error in java.How i get return value.
thanks
use the code I posted, not the first example from the link....that first example sucks :-(
-----------------
public class inJava extends Applet
{
public void init()
{
System.out.println( callBrowser( "getText()" ) ) ;
}
public String callBrowser( String jsCmd )
{
String jsResult = null ;
boolean success = false ;
try
{
Method getw = null, eval = null ;
Object jswin = null ;
/* does it in IE too */
Class c = Class.forName( "netscape.javascript.JSObj
Method ms[] = c.getMethods() ;
for( int i = 0 ; i < ms.length ; i++ )
{
if( ms[ i ].getName().equals( "getWindow" ) )
getw = ms[ i ] ;
else if( ms[ i ].getName().equals( "eval" ) )
eval = ms[ i ] ;
}
Object a[] = new Object[ 1 ] ;
a[ 0 ] = this ; /* this is the applet */
jswin = getw.invoke( c, a ) ; /* this yields the JSObject */
a[ 0 ] = jsCmd ;
Object result = eval.invoke( jswin, a ) ;
if( result instanceof String )
jsResult = (String)result ;
else
jsResult = result.toString() ;
success = true ;
}
catch( InvocationTargetException ite )
{
jsResult = "" + ite.getTargetException() ;
}
catch( Exception e )
{
jsResult = "" + e ;
}
if(success)
return jsResult ;
else
{
System.out.println( "eval failed with error " + jsResult ) ;
return null ;
}
}
}
-------------------
<HTML><HEAD></HEAD>
<BODY>
<SCRIPT>
function getText()
{
return document.getElementById("U
}
</SCRIPT>
<APPLET CODE="inJava.class"
NAME="myApplet" MAYSCRIPT
HEIGHT=400 WIDTH=10>
</APPLET>
<br>
<div id="UserText">Woo woo!!</div>
<!-- div may be this...can't remember -->
<div name="UserText">Woo woo!!</div>
</BODY>
</HTML>
hi tim
I am bit confused with callbrowser function.
I am not getting what is going on.
I make you clear that ,In javascript I have to function.In one function,I am passing one String argument and in another function I have to receive the String value.
function setText(myText)
{
document.getElementById("T
}
function getText()
{
return document.getElementById("U
}
how I will use these function in javascript.
what I get ,for returning function is that,inside callbrowser function you are receiving value in result.Is I am right?
Please tim,try to understabd me.I don't know javascript.
thanks
TimYates
I have some problem.if you try to help me.I am not gettin about the callbrowser.Is it system defined or user defined.PLease try to help me timYates.
I am posted here my full program with comments.Hope you will understand.
java file:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.Socket;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.TextField;
/*<applet code="support" width=450 height=260>
<param name=host value ="100.100.7.27">
<param name=port1 value="8888">
</applet>*/
public class Csupport extends Applet implements Runnable
{
Button sendData;
boolean ClientStatusmsg;
Thread t;
byte[]linebuffer=new byte[3000];
OutputStream opst,opstchat;
String opratName;
String outputData;
String outputDatatemp;
String msgconnect;
InputStream ipst;
Socket sock;
String oprttyping;
String host1,port1;
int len;
public void init()
{
try
{
sendData=new Button("Send");
add(sendData);
sendData.setEnabled(false)
ClientStatusmsg=false;
host1=getParameter("host")
port1=getParameter("port1"
}
catch(Exception e){
System.out.println(e);
}
}
public void start()
{
try
{
int port=8888;
String sdg=new String();
linebuffer = new byte[3000];
System.out.println("Port Value Start" +port1);
System.out.println("host Value Start" +host1);
sock=new Socket("100.100.7.27",8888
//sock=new Socket("65.61.162.30.",888
opst =sock.getOutputStream();
t = new Thread(this);
t.start();
sdg="CASE2001:^~%";
sdg=sdg+host1 +"@#";
System.out.println("Data from sdg"+ sdg);
len=sdg.length();
linebuffer=sdg.getBytes();
if(len>linebuffer.length-1
len = linebuffer.length-1;
opst.write(linebuffer, 0, len+1);
opst.flush();
}//end of try
catch(Exception e) {
//userChat.appendText("app
}//end of catch
}//end of start function
//function to get input data
public void run()
{
String temp = new String();
String temp2=new String();
String temp1;
outputData=new String();
outputDatatemp=new String();
byte []buf1=new byte[3000];
try
{
//ipst.reset();
ipst = sock.getInputStream();
byte[] buf = new byte[3000];
while(true)
{
int avail = ipst.available();
if(avail<1) avail=1;
if(avail>buf.length)
{
avail=buf.length;
}
int bytes_read = ipst.read(buf,0,avail);
int j = 0;
int i;
for( i=0; i<=bytes_read; i++)
{
buf[j++] = buf[i];
}
temp1 = new String(buf,0,0, j);
temp2 = temp2 + temp1;
temp2=temp2.trim();
System.out.println("temp1 :" +temp1);
if(j>bytes_read)
{
System.out.println("temp2=
temp.trim();
System.out.println("temp :" +temp);
if(temp1.length()>3)
{
//int hh=temp1.indexOf (0,10);
outputData=temp1.substring
outputDatatemp=temp2.subst
System.out.println("output
System.out.println("output
//opratName.substring(
//Message when operator accept the chat
if((outputData.equals("ASE
{
String accp_msg=new String();
int pp=temp1.indexOf ("{",0);
String tempoprtname=new String();
//pp=Integer.parseInt(outp
System.out.println(pp);
accp_msg=temp1.substring(1
int txtlen=accp_msg.length();
txtlen=txtlen+18;
int pp2=temp1.indexOf ("$",txtlen);
tempoprtname=temp1.substri
opratName=tempoprtname;
System.out.println("output
System.out.println("opratN
msgconnect=accp_msg +":" + tempoprtname;
System.out.println("Accept
try {
getAppletContext().showDoc
(new URL("javascript:setText(\"
System.out.println("Accept
}
catch (MalformedURLException me) {System.err.print(me); }
//dsend.setEnabled(true);
sendData.setEnabled(true);
//userText.setEditable(tru
temp1=" ";
outputData=" ";
temp2=" ";
msgconnect=" ";
tempoprtname=" ";
}
System.out.println("Output
// start chattin
if((outputData.equals("ASE
{
String strTemp=new String();
String strColor=new String();
System.out.println("temp1 in ASE1011"+ temp1);
int pp=temp1.indexOf ("^",0);
int lp=temp1.length();
System.out.println("Value of P is "+pp);
strTemp=temp1.substring(pp
System.out.println("Chat data to send html"+strTemp);
//here I have to send strTemp data in javasript function
try {
getAppletContext().showDoc
(new URL("javascript:setText(\"
System.out.println("Chat data to send html"+strTemp);
}
catch (MalformedURLException me) {System.err.print(me); }
//strTemp="";
temp1="";
outputData="";
temp2="";
strTemp="";
}
}//end of temp length if
}//end of outer if
}//END OF WHILE
//System.out.println("Temp
} catch(Exception e) {
System.err.print(e);
}
}
//////////////////////////
//handle mouse event to click on button
public boolean handleEvent(Event e) {
if((e.id==Event.ACTION_EVE
{
byte[]linebuffer=new byte[3000];
String ClientChat="CASE2002:^~#%"
String line =new String();//= getText;
//here I have to receive the value.
/*try {
//line=getAppletContext().
}
catch (MalformedURLException me) {System.err.print(me);}*/
if(line.length()>0)
{
System.out.println("Value of line " +line);
line=port1+": "+line;
ClientChat=new String(ClientChat+opratNam
System.out.println("Value to send"+ClientChat);
//userChat.appendText(">>"
System.out.println("Value to sendcheck exception"+ ClientChat);
int len = ClientChat.length();
linebuffer=ClientChat.getB
if(len>linebuffer.length-1
len = linebuffer.length-1;
linebuffer[len]='\n';
System.out.println("Hello"
try {
//opst=sock.getOutputStrea
System.out.println("in trytest");
opst.write(linebuffer, 0, len);
opst.flush();
} catch(Exception ex) {
System.err.print(ex);
}
//userText.setText("");
ClientChat=" ";
line="";
}//end of String length if
}//end of if
return false;
}//end of function
}
//trap cursor foucs
Please Tim try to help me.
thanks
I am gettin this error for gettext function.
java.net.MalformedURLExcep
eval failed with error== netscape.javascript.JSExce
null
eval failed with error== netscape.javascript.JSExce
null
the callBrowser function just uses reflection to call a piece of javascript in the same HTML as the applet :-)
You can do it without reflection, but then you need to add java40.jar to the classpath when you compile, as the compiler will not be able to find the JSObject class otherwise...
That's why it's easier to use reflection :-)
Tim
> java.net.MalformedURLExcep
What are you doing? That's not an error from my code...
are you still doing showDocument() ?
Get rid of it...
Just use callBrowser()...
CHANGE:
-----------------
try {
getAppletContext().showDoc
(new URL("javascript:setText(\"
System.out.println("Accept
}
catch (MalformedURLException me) {System.err.print(me); }
TO
-----------------
callBrowser( "setText( '" + msgconnect + "' )" ) ;
(make sure you have added the callBrowser method to your applet class)
Tim
> Please give me for both function.String return type with argument and void reurn type with String argument.
I have shown you twice already...
void return type, string argument:
--------------------------
callBrowser( "setText( 'Hello' )" ) ;
string return type, string argument:
--------------------------
String s = callBrowser( "getText( 'Hello' )" ) ;
string return type, no argument:
--------------------------
String s = callBrowser( "getText()" ) ;
The callBrowser method (again) that you need to add to your applet class is:
--------------------------
public String callBrowser( String jsCmd )
{
String jsResult = null ;
boolean success = false ;
try
{
Method getw = null, eval = null ;
Object jswin = null ;
/* does it in IE too */
Class c = Class.forName( "netscape.javascript.JSObj
Method ms[] = c.getMethods() ;
for( int i = 0 ; i < ms.length ; i++ )
{
if( ms[ i ].getName().equals( "getWindow" ) )
getw = ms[ i ] ;
else if( ms[ i ].getName().equals( "eval" ) )
eval = ms[ i ] ;
}
Object a[] = new Object[ 1 ] ;
a[ 0 ] = this ; /* this is the applet */
jswin = getw.invoke( c, a ) ; /* this yields the JSObject */
a[ 0 ] = jsCmd ;
Object result = eval.invoke( jswin, a ) ;
if( result instanceof String )
jsResult = (String)result ;
else
jsResult = result.toString() ;
success = true ;
}
catch( InvocationTargetException ite )
{
jsResult = "" + ite.getTargetException() ;
}
catch( Exception e )
{
jsResult = "" + e ;
}
if(success)
return jsResult ;
else
{
System.out.println( "eval failed with error " + jsResult ) ;
return null ;
}
}
Thanks a lot tim.Please help once again.
the return value I am not gettin g.It is giving undefined.I am using in this way.
in java
String d=new String();
d=( callBrowser( "getText()" ) ) ;
System.out.println("return
in java script
function getText()
{
var s = "PP";
return s;
}
other call browser function is the same.I am getting the result of void return type, string argument:
One more thing Tim,when ever I change in java file,i doesn't reflect on html file,until I recompile java file in different name .why this problem occur.I am using IE5.
Thanks.Once again.
Business Accounts
Answer for Membership
by: TimYatesPosted on 2003-12-12 at 01:23:21ID: 9926905
Add this method to your applet class:
adetails/j ava-0172.h tml
ect" ) ;
// Cool reflection based javascript calling method
// Taken from http://www.rgagnon.com/jav
public String callBrowser( String jsCmd )
{
String jsResult = null ;
boolean success = false ;
try
{
Method getw = null, eval = null ;
Object jswin = null ;
/* does it in IE too */
Class c = Class.forName( "netscape.javascript.JSObj
Method ms[] = c.getMethods() ;
for( int i = 0 ; i < ms.length ; i++ )
{
if( ms[ i ].getName().equals( "getWindow" ) )
getw = ms[ i ] ;
else if( ms[ i ].getName().equals( "eval" ) )
eval = ms[ i ] ;
}
Object a[] = new Object[ 1 ] ;
a[ 0 ] = this ; /* this is the applet */
jswin = getw.invoke( c, a ) ; /* this yields the JSObject */
a[ 0 ] = jsCmd ;
Object result = eval.invoke( jswin, a ) ;
if( result instanceof String )
jsResult = (String)result ;
else
jsResult = result.toString() ;
success = true ;
}
catch( InvocationTargetException ite )
{
jsResult = "" + ite.getTargetException() ;
}
catch( Exception e )
{
jsResult = "" + e ;
}
if(success)
return jsResult ;
else
{
System.out.println( "eval failed with error " + jsResult ) ;
return null ;
}
}
then, call the method with:
callBrowser( "MsgBox( '" + msg + "' )" ) ;
(make sure your applet is in the same HTML as the javascript
Tim