flash as的ExternalInterface.call()实现有bug,对象互转不完全

如简单的尝试ExternalInterface.call('alert', {d:'\\'}),就会导致js出错,


有人专门做了个尝试


http://mihai.bazon.net/blog/externalinterface-is-unreliable



"ExternalInterface is unreliable"


简单的解决方案是使用下面的方式互相转换,之前我使用过escape与unescape



The solution

Beware, this is very ugly. :-)  The solution is to take the problem in our own hands and therefore encode dangerous characters ourselves, before sending them to JS.  I determined the following characters to be problematic: backslash (\), quote (") and ampersand (&).  Here's my method of dealing with it:

data = data.split("%").join("%25")
           .split("\\").join("%5c")
           .split("\"").join("%22")
           .split("&").join("%26");

// safe to send data now
ExternalInterface.call("alert", data);

So I chosen to encode special chars using the “%XX” notation.  I'm encoding “%” first, so we can safely use it for other characters.  Then backslash, then quotes, then ampersand.

Note how complex it has to be because Flash (8 at least) doesn't support RegExp-s, nor does it have a string.replace function.

On the JS side, when I receive data I'm doing the reverse transformation:

data = data.replace(/%22/g, "\"")
           .replace(/%5c/g, "\\")
           .replace(/%26/g, "&")
           .replace(/%25/g, "%");

A different solution I found on some sites was to use escape() in ActionScript, and unescape() in JavaScript.  This should be theoretically faster since it's one function call (and there's a good chance that this function is implemented in low level C); however, Unicode characters get mangled in translation (curiously, this is a documented bug).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值