zaterdag 30 oktober 2010

Calling Java from Javascript and calling Javascript from Java




This little sample shows that we can call javascript doAlert from Java.
Also we show that calling the function display will be received in Java with
data from Javascript.


applet.java


import java.applet.*;
import java.net.*;


public class applet extends Applet{
public void init(){
Display("start of java");
}
public void Display(String txt){
try{
getAppletContext().showDocument(new URL("javascript:doAlert(\""+txt+"\")"));
}catch(Exception e){}
}
}

index.html



<HTML>
<SCRIPT>
function doAlert(s) {
   alert(s);
}
</SCRIPT>
<APPLET CODE="applet.class" NAME="applet"  MAYSCRIPT >
</APPLET>
<INPUT type="button" value="Java.Display" onClick="document.applet.Display('hi')">
</HTML>

Geen opmerkingen:

Een reactie posten