js与Flex交互,SharedObject存储值
如:
1、IE存,Firefox取 OK
2、Firefox存,IE取 OK
3、IE存,Chrome取 NULL
4、FireFox存,Chrome取 NULL
5、Chrome存,Chrome取 OK
Flex存储值 应该和浏览器没有关系的吧,为什么会出现这种情况?
下面是我的代码:
import flash.external.ExternalInterface; import mx.controls.Alert; import mx.core.Application; private var mySo:SharedObject; private function init():void{ ExternalInterface.addCallback("flexFun",flexFunTest); ExternalInterface.call("jsFunTest"); mySo = SharedObject.getLocal("application-name"); } private function flexFunTest(str:String):String{ var message:String = ""; if(str.length > 0){ mySo.data.setValue = str; var minDiskSpace:int = 500; writeDiskSpace(minDiskSpace); //写入本地文件 message = "<" + str + ">值保存成功2!"; } else{ str = mySo.data.setValue; message = "<" + str + ">取值成功2!"; } return "调用了Flex函数," + message; } private function writeDiskSpace(minDiskSpace:int):void{ var str:String = mySo.flush(minDiskSpace); switch(str){ case SharedObjectFlushStatus.FLUSHED: // 写入本地文件成功 break; case SharedObjectFlushStatus.PENDING: //磁盘空间不足 扩展磁盘空间 minDiskSpace = minDiskSpace * 2; writeDiskSpace(minDiskSpace); break; } }
window.onload = function () { document.getElementById("swfHtml").innerHTML = getSWFHTML(); } function callApp() { var str = document.getElementById("txtName").value; var x; if (navigator.appName.indexOf("Microsoft") != -1) { x = document.getElementById("MyFlexApp").flexFun(str); } else { x = document.getElementById("MyFlexApp").flexFun(str); } alert(x); } function getSWFHTML() { var swfNode = ""; if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // Netscape浏览器插件体系结构 swfNode = '<embed type="application/x-shockwave-flash" src="flex/FlexProject2.swf" '; swfNode += 'id="MyFlexApp" name="MyFlexApp" '; swfNode += 'allowScriptAccess="sameDomain" '; swfNode += 'FlashVars ="param1=one¶m2=2¶m3=3¶m4=four"'; swfNode += '/>'; } else { // IE swfNode = '<object id="MyFlexApp" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">'; swfNode += '<param name="movie" value="flex/FlexProject2.swf" />'; swfNode += '<param name="FlashVars" value="param1=one¶m2=2¶m3=3¶m4=four" />'; swfNode += '<param name="allowScriptAccess" value="sameDomain" />'; swfNode += "</object>"; } return swfNode; } function jsFunTest() { alert("调用了JS函数"); }
<div id="swfHtml" style="height:300px; margin-bottom:50px;"> </div> <input type="text" id="txtName" /> <input onclick="callApp();" value="调用Flex" type="button" />