cha*_*dra 3 javascript flash firefox
HI,
我试图从javascript(firefox-3.0/Linux)调用flash对象中定义的方法并获得异常:"NPMethod调用非NPObject包装的JSObject".
如果我在window.document.flash_object.func()上使用eval,则会抛出"NPMethod调用非NPObject包装的JSObject".
如果,如果我在页面的侧面定义一个javascript函数,如下所示:
function myFunc(){return flash_object.func();
}
然后做一个window.document.myFunc()的eval工作正常.
我通过一个名为Selenium的测试框架来运行这两个版本.[eval(window.document.flash_object.func())和eval(window.document.myFunc())].
问题似乎是在不传递'this'引用的情况下调用flash-object方法的问题.下面是重现此问题的示例html/js代码:"NPMethod调用非NPObject包装的JSObject".
<script>
function changeColor() {
mymovie.changeColor();
}
function getColorNP() {
var func = mymovie.getColor;
func();
}
</script>
<button type="button" onclick="getColorNP();">getColorNP</button>
<button type="button" onclick="getColor();">getColor</button>
getColorNP throws the exception
Error: NPMethod called on non-NPObject wrapped JSObject!
Source File: http://my.host/Colors/colors.html
getColorNP throws the exception
Error: NPMethod called on non-NPObject wrapped JSObject!
Source File: http://my.host/Colors/colors.html
Run Code Online (Sandbox Code Playgroud)
现在,向javascript专家提问:给定flash对象和方法名称,如何在该对象上调用该方法.可以说,一个函数有两个参数:一个flash对象,一个方法名称为string.我想在该函数内的object.method()上做一个eval.这是可能的,如果可以的话,请你解释一下如何做到这一点.
由于flash对象的方法不是标准的javascript函数,我认为它不可能通过bind()进行绑定.还有其他选择吗?
谢克斯,钱德拉
当您尝试将flash函数(本机代码)设置为某个变量然后调用此变量时,会发生此错误.举个例子来看看你的功能:
function getColorNP() { // this will call error
var func = mymovie.getColor;
func();
}
function getColorNP() { //this will work
mymovie.getColor();
}
Run Code Online (Sandbox Code Playgroud)
如果你需要将flash函数保存到某个变量,你应该用lambda函数包装它.像这样:
function getColorNP() { // this will work also
var func = function(){mymovie.getColor()};
func();
}
Run Code Online (Sandbox Code Playgroud)
所以当你在javascript中使用flash功能时,你不应该制动链接链.