从小程序调用 JS 可以在 Firefox &Chrome 但不是 Safari [英] Calling JS from an applet works in Firefox & Chrome but not Safari
问题描述
我在小程序中有以下代码来调用一些 Javascript(这有点令人费解,因为被调用的 fn 从 divId 标识的 DOM 中获取一个对象并在其上调用一个函数).
I have the following code in an applet to call some Javascript (it's a bit convoluted because the fn that's called gets an object from the DOM identified by divId and calls a function on it).
@Override
public final void start() {
System.err.println("start() method called");
this.javascript = JSObject.getWindow(this);
this.jsObjectDivId = getParameter("parent_div_id");
this.initCallbackFnName = getParameter("init_callback");
Object args[] = {this.jsObjectDivId, this.initCallbackFnName};
System.out.print("Calling init_callback\n");
this.javascript.call("callJS", args);
}
callJS
函数是:
window.callJS = function(divId, functionName, jsonArgString) {
var args, obj;
obj = $(divId).data('neatObject');
args = eval(jsonArgString);
return obj[functionName](args);
};
在 Firefox/Chrome 中,divId
和 functionName
参数包含有效的字符串并且一切正常;在挂在指定 DIV 数据上的对象上调用所需的函数.
In Firefox/Chrome the divId
and functionName
arguments contain valid strings and everything works fine; the desired function is called on the object hanging off the specified DIV data.
在 Safari 中,divId
和 functionName
参数都报告为 JavaRuntimeObject
,其值为 true
.
In Safari, the divId
and functionName
arguments are both reported as a JavaRuntimeObject
with values of true
.
> divId
JavaRuntimeObject
true
是什么?
推荐答案
并非所有浏览器都完全支持 LiveConnect.特别是,当使用 call
时,Safari 不会将 Java 字符串转换为探测器 JS 等效项.在您的情况下,您可以在 Applet 端使用 eval
而不是 call
并放入带有参数的 JSON 字符串对象.类似的东西:
LiveConnect is not fully supported in all browsers. Especially, Safari doesn't convert Java Strings to the prober JS equivalent when using call
. In your case you can just use eval
at the Applet side instead of call
and put in a JSON string object with the arguments. Something like:
javascript.eval(callback + "({\"id\":\"" + id + "\",\"
... })")
基本上,您需要知道有效的 LiveConnect 的跨浏览器兼容子集.我写了一篇描述子集的博客文章:http://blog.aarhusworks.com/applets-missing-information-about-liveconnect-and-deployment/
Basically, you need to know the cross-browser compatible subset of LiveConnect that works. I've written a blog post that describes the subset: http://blog.aarhusworks.com/applets-missing-information-about-liveconnect-and-deployment/
它带有在浏览器中运行的 LiveConnect 测试套件:http://www.jdams.org/live-connect-test
It comes with a LiveConnect test suite which runs in the browser: http://www.jdams.org/live-connect-test
这篇关于从小程序调用 JS 可以在 Firefox &Chrome 但不是 Safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!