본문 바로가기

카테고리 없음

Applet에서 자바스크립트 파라미터없는 함수호출 예제

import java.applet.Applet;
import java.awt.*;
import netscape.javascript.*;

public class MyApplet extends Applet {
 private JSObject window = null;      //stores the window
 private JSObject document = null;    //stores the document

 public void init() {
  this.setBackground(Color.ORANGE);
  /* Get the JS window */
     window = JSObject.getWindow( this );
     /* Get the JS document */
     document = (JSObject) window.getMember( "document" );
 }

 public void start() {
  JSObject form1 = (JSObject)document.getMember("form1");
  JSObject text = (JSObject)form1.getMember("message");

  String value = (String)text.getMember("value");
  value = "Applet에서 HTML Form에 접근하여 가져온 데이터:"+value;
  value = "Applet에서 Javascript sampleFunc()함수를 호출함\n"+value;
  window.call("sampleFunc", null);
 }

 public void stop() {}

 public void destroy() {}

 public void paint(Graphics g) {
  g.drawString("Hello World", 20, 20);
 }
}


 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Applet Demo</title>

<script>
// Applet으로부터 호출되는 함수선언
function sampleFunc(){
 alert("Applet에서 Javascript:sampleFunc()함수를 호출하였습니다");
}

</script>

</head>
<body>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<object
    classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
    codebase = "http://java.sun.com/update/1.6.0/jinstall-6u30-windows-i586.cab#Version=6,0,0,5"
    WIDTH = "200" HEIGHT = "50" >
    <PARAM NAME = CODE VALUE = "MyApplet" >
    <param name = "type" value = "application/x-java-applet;version=1.6">
    <param name = "scriptable" value = "true">
    <comment>
 <embed
            type = "application/x-java-applet;version=1.6" \
            CODE = "MyApplet" \
            WIDTH = "200" \
            HEIGHT = "50"
     scriptable = true
     pluginspage = "http://java.sun.com/products/plugin/index.html#download">
     <noembed>
           
            </noembed>
 </embed>
    </comment>
</object>

<!--
<APPLET CODE = "MyApplet" WIDTH = "200" HEIGHT = "50">


</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
<form name="form1">
테스트용 폼 데이터:<input type="text" name="message" value="Hello World">
</form>
</body>
</html>



사용자 삽입 이미지