我的代码是
function getID( swfID ){
if(navigator.appName.indexOf("Microsoft") != -1){
me = window[swfID];
}else{
me = document[swfID];
}
}
function js_to_as( str ){
me.onChange(str);
}
然而,有时我的onChange不加载。Firebug错误
我。onChange不是一个函数
我想优雅地降级,因为这不是我的程序中最重要的特性。Typeof给出相同的错误。
关于如何确保它存在,然后只执行onChange有什么建议吗?
(下面的方法除了try catch one work之外都没有)
这里有一个工作和简单的解决方案,检查一个函数的存在性,并由另一个函数动态触发该函数;
触发函数
function runDynamicFunction(functionname){
if (typeof window[functionname] == "function") { //check availability
window[functionname]("this is from the function it"); // run function and pass a parameter to it
}
}
现在可以用PHP动态生成函数
function runThis_func(my_Parameter){
alert(my_Parameter +" triggerd");
}
现在可以使用动态生成的事件调用该函数
<?php
$name_frm_somware ="runThis_func";
echo "<input type='button' value='Button' onclick='runDynamicFunction(\"".$name_frm_somware."\");'>";
?>
你需要的HTML代码是
<input type="button" value="Button" onclick="runDynamicFunction('runThis_func');">