使用javascript调用Applet.getMethod()会抛出错误消息:TypeError:Applet.getMethod()不是函数

使用javascript调用Applet.getMethod()会抛出错误消息:TypeError:Applet.getMethod()不是函数,javascript,firefox,windows-7,applet,jmf,Javascript,Firefox,Windows 7,Applet,Jmf,我有一个使用JMF库的小程序,叫做: <object id="cameraViewer" classid="java:MyApplet.class" type="application/x-java-applet" archive="myapplet.jar" height="197" width="159" align="middle" codebase="."> <param name="code" value="My

我有一个使用JMF库的小程序,叫做:

<object id="cameraViewer"
    classid="java:MyApplet.class"
    type="application/x-java-applet"
    archive="myapplet.jar" height="197" width="159"
    align="middle" codebase=".">
    <param name="code"
        value="MyApplet" />
    <param NAME="MAYSCRIPT" VALUE="true" />
    <param name="appletWidth" value="250" />
    <param name="appletHeight" value="200" />
    <param name="archive" value="myapplet.jar" />
    <param name="JAVA_CODEBASE" value="." />
    <font color="red">Applet error</font>
</object>
在javascript代码的第二行中,javascript控制台中抛出了一个错误(TypeError:cameraViewer.listDevices不是函数)

只有当我将Windows7与Firefox 8.0.1一起使用时,才会引发此问题

因为此代码可以很好地用于:

  • Windows7和Chrome
  • Windows7和Firefox20
  • Windows XP和Firefox 8.0.1

你知道这个问题吗

我认为您试图在函数尚未加载时调用该函数(浏览器在加载小程序时表现不同,一些浏览器同步加载,而另一些浏览器不同步加载)

在尝试调用该函数之前,您可以更安全地检查该函数是否存在,如果不存在,请告诉浏览器等待几毫秒

下面是一个模拟代码:

    var cameraViewer = document.getElementById('cameraViewer');

    if (typeof(cameraViewer.listDevices) != "undefined") { 
    // safe to use the function
    var deviceList = new Array(cameraViewer.listDevices());
}
else{
  setTimeout(function() {
    var deviceList = new Array(cameraViewer.listDevices());
  }, 1000);
}

谢谢你的回答,给了我一个使用睡眠方法的想法。所以在我的例子中,我鼓励使用try-catch块来解决这个问题,当捕获异常时,我会在somme毫秒后使用(setTimeout方法)重新调用我的函数。
    var cameraViewer = document.getElementById('cameraViewer');

    if (typeof(cameraViewer.listDevices) != "undefined") { 
    // safe to use the function
    var deviceList = new Array(cameraViewer.listDevices());
}
else{
  setTimeout(function() {
    var deviceList = new Array(cameraViewer.listDevices());
  }, 1000);
}