AS3 javascriptを使用せずにactionscriptのみでブラウザ判別
- 時間:2009年12月18日 17:39
-
delicious に登録
-
はてブに登録
- トラックバック:(0)
- コメント:(0)
javascript側に関数を用意して、swfからその関数を呼び出してブラウザ判別する方法はよくしていましたが、
htmlファイルをいじらずにswfのみで同じことが出来ないかなー思っていたら出来るみたいなのでメモ。
htmlファイルが何らかの理由でさわれない時に重宝しそう・・・。
下記のサンプルswfに現在このサイトを見ているブラウザ名が表示されていれば成功。
【サンプルswf】
【Sample20091218.as】
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.external.ExternalInterface;
/*
* 【参考サイト】
* http://blog.air-life.net/2008/12/flex.html
* http://flex4web.sblo.jp/article/26894378.html
*
*/
public class Sample20091218 extends Sprite {
private var _debugTxt:TextField;
public function Sample20091218():void {
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_debugTxt= new TextField();
_debugTxt.autoSize = TextFieldAutoSize.CENTER;
_debugTxt.selectable = false;
_debugTxt.x = stage.stageWidth / 2 - _debugTxt.width / 2;
_debugTxt.y = stage.stageHeight / 2 - _debugTxt.height / 2;
addChild(_debugTxt);
_init();
}
private function _init():void {
_debugTxt.text = "_init";
if (ExternalInterface.available) {
var str:String = ExternalInterface.call("window.navigator.userAgent.toLowerCase");
if (str.indexOf("msie") > -1) {
_debugTxt.text = "IE";
}else if (str.indexOf("firefox") > -1) {
_debugTxt.text = "Firefox";
}else if (str.indexOf("opera") > -1) {
_debugTxt.text = "opera";
}else if (str.indexOf("netscape") > -1) {
_debugTxt.text = "netscape";
}else if (str.indexOf("chrome") > -1) {
_debugTxt.text = "Chrome";
}else {
_debugTxt.text = "Unknown";
}
}else {
_debugTxt.text = "EROOR";
}
}
}
}
【参考サイト】
AS3 javascriptを使用せずにactionscriptのみでブラウザ判別に対してコメントをする