Firefox使用JavaScript調(diào)用flash中注冊的方法時,如果此方法通過js函數(shù)返回后執(zhí)行,而不是直接通過swf容器獲取到后再執(zhí)行,那么在Firefox下會出現(xiàn)“NPMethod called on non-NPObject wrapped JSObject!”錯誤,其他瀏覽器如chrome,ie不報錯。
解決辦法就是直接通過swf的容器獲取到flash中注冊的方法后執(zhí)行,而不是通過js方法返回flash中注冊的方法。
測試代碼如下
as腳本
function loadPicture(){//......代碼......//......代碼......}flash.external.ExternalInterface.addCallback("forJS", null, loadPicture);//注冊JavaScript中調(diào)用的回調(diào)函數(shù)HTML測試代碼
<script type="text/javascript">document.write('<div class="dvswf">' + (document.all ? '<object id=swfId classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000><param name="src" value="/images/loadSwf.swf"></object>': '<embed id="swfId" type="application/x-shockwave-flash" src="/images/loadSwf.swf"/>') + '</div>');function $(Id){return document.getElementById(Id);}function getFlashMethod(swfID, container) { if (window[swfID] && window[swfID].forJS) return container ? window[swfID] : window[swfID].forJS; if (document[swfID] && document[swfID].forJS) return container ? document[swfID] : document[swfID].forJS; if ($(swfID).forJS) return container ? $(swfID) : $(swfID).forJS; return false;}function callswfmethod(container) { var o = getFlashMethod('swfId', container); if (o) {//能獲取到注冊的JavaScript回調(diào)方法或者swf容器 container ? o.forJS()//為容器時從容器中直接獲取flash.external.ExternalInterface.addCallback注冊的函數(shù)然后執(zhí)行Firefox和其他瀏覽器未報錯 : o(); //如果返回的是flash.external.ExternalInterface.addCallback注冊的函數(shù),firefox 執(zhí)行此方法會出現(xiàn)“NPMethod called on non-NPObject wrapped JSObject!”錯誤,其他瀏覽器如chrome,ie不報錯 }}</script> <input type="button" value="調(diào)用flash.external.ExternalInterface.addCallback注冊的函數(shù)--返回函數(shù)" onclick="callswfmethod()" /><input type="button" value="調(diào)用flash.external.ExternalInterface.addCallback注冊的函數(shù)--返回flash容器" onclick="callswfmethod(true)" />新聞熱點
疑難解答