なんか最近 FlashPlayer がアップデートされて
IE で ExternalInterface を使った時に文字化けするという話をよく聞くので少し調べてみた。
※ 明らかなバグだと思うのでそのうち直るだろうけど気持ち悪いので
条件
- IE 用のFlashPlayer
- ExternalInterface.call を使用して JS を呼んでいる
- 引数にマルチバイト文字列が含まれている
現象
- 文字化け
- 文字化けによりJSエラー
対処法
JS にマルチバイト文字列を渡さない
マルチバイトかどうかは
if (/^[^\u0001-\u007D]+$/.test(text))
{
trace("マルチバイトがふくまれてるよ");
}
こんなんでざっくりチェックが出来る。
かなり適当だけど化けそうな文字が判定できればいいのでよしとする。
でも日本語渡したい
って人は IE だけ少し特別な処理にする。
if (ExternalInterface.call("window.navigator.userAgent.toLowerCase").indexOf("msie") >= 0)
{
// IE 用の処理
}
って感じに IE を分ける。
中ではどんなことをするかというと
var objectID: String = ExternalInterface.objectID;
var functionName: String = "moja";
var param: String = "\"あいうえお\"";
navigateToURL(new URLRequest(
"javascript:document.getElementById('" +
objectID + "').SetReturnValue(eval('try{__flash__toXML(" +
functionName + "(" + param + "));}catch(e){\"<undefined/>\";}'))"
), "_self");
こんな感じでやると普通に日本語も渡せた。
問題点
この方法で呼ぶと
var res:String = ExternalInterface.call("moja");
のように戻り値を受け取ることが出来ない。
でも戻り値ほしい
AS側から Base64 でエンコードしたやつを ExternalInterface.call で渡す。
そのへんから Base64 をデコードできるやつを拾ってきて JS 側で Base64 をデコードする。
気が向いたら他にも調べてみる。