JavaScript和Applet通信

1、Javascript要访问的Applet的方法或属性必须都是public的 

2、在html中给applet的name属性赋值,以前是用applet标签,现在要用object,也是一样,如下(看红色name属性): 
< OBJECT
    classid 
=   " clsid:8AD9C840-044E-11D1-B3E9-00805F499D93 "
    codebase 
=   " http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,7 "
    WIDTH 
=   600  HEIGHT  =   470  name = " browseFileApplet " >
    
< PARAM NAME  =  CODE VALUE  =   " com.jointforce.applet.browsefile.BrowseFileApplet.class "   >
    
< PARAM NAME  =  ARCHIVE VALUE  =   " browsefileapplet.jar "   >
    
< PARAM NAME  =   " type "  VALUE  =   " application/x-java-applet;version=1.4 " >
    
< PARAM NAME  =   " scriptable "  VALUE  =   " false " >
    
< PARAM NAME  =  username VALUE = " <%=username%> " >
    
< PARAM NAME  =  socketserverip VALUE = " <%=ssip%> " >
    
< PARAM NAME  =  socketserverport VALUE = " <%=ssport%> " >
    
< PARAM NAME  =  sockettimeout VALUE = " <%=sto%> " >
    
< PARAM NAME  =  userhomedirectory VALUE = " <%=uhd%> " >

    
< COMMENT >
< EMBED
            type 
=   " application/x-java-applet;version=1.4 "  \
            CODE 
=   " com.jointforce.applet.browsefile.BrowseFileApplet.class "  \
            ARCHIVE 
=   " browsefileapplet.jar "  \
            WIDTH 
=   600  \
            HEIGHT 
=   470  \
            name 
=   " browseFileApplet "  \
            username 
= " <%=username%> "  \
            socketserverip 
= " <%=ssip%> "  \
            socketserverport 
= " <%=ssport%> "  \
            sockettimeout 
= " <%=sto%> "  \
            userhomedirectory 
= " <%=uhd%> "  \
     scriptable 
=   false  \
     pluginspage 
=   " http://java.sun.com/products/plugin/index.html#download " >
     
< NOEMBED >
    
            
</ NOEMBED >
</ EMBED >
    
</ COMMENT >
</ OBJECT >

 

3、在javascript中直接就可以访问了,如下: 

function okbtnpressed() {
    
//  current selection is a directory or a file? getCurrentSel是applet的一个方法
    var currentSel  =  window.document.browseFileApplet.getCurrentSel();
    
if  (currentSel  ==   " OK " ) {
        window.opener.document.SubmitTaskForm.executable.value 
=  window.document.browseFileApplet.result;   // result是applet的一个成员变量
        window.opener  =   null ;
        
return  window.close();
        
return   false ;
    } 
else  {
        alert(
" 请选择一个文件! " );
        
return   false ;
    }
}

 

这种方法只在IE中有效,在Firefox中无效。后面将有一篇文章谈到如何在Firefox和IE中都能让JavaScript和JAVA通讯。 

你可能感兴趣的:(JavaScript)