ブラウザにメッセージを送信する

 Flashのコンテンツからブラウザにメッセージを送信する方法です。ここでは、Flashからmessageboxコマンドを送信して、ブラウザ側のJavascriptのalertメソッドを使って警告ダイアログを表示します。


Flashファイルのボタンに以下のスクリプトを追加します。

on (press) {
 fscommand("messagebox", "This is a message box invoked from within Flash.");
}

HTML パブリッシュ設定のテンプレートをFlash(FSCCommandサポート)に設定して、パブリッシュします。

書き出されたHTMLファイルにはfscommand コマンドを受信するためのJavaScriptが追加されています。
書き出し直後のHTMLはこのようになっています。「fscomd」部分はswfファイルのファイル名となっています。この部分は変更しないようにしてください。

<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html; charset=SHIFT_JIS">
<TITLE>fscomd</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<SCRIPT LANGUAGE=JavaScript>
<!--
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the the FSCommand messages in a Flash movie
function fscomd_DoFSCommand(command, args) {
var fscomdObj = InternetExplorer ? fscomd : document.fscomd;
//
// Place your code here...
//
}
// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
document.write('<SCRIPT LANGUAGE=VBScript\> \n');
document.write('on error resume next \n');
document.write('Sub fscomd_FSCommand(ByVal command, ByVal args)\n');
document.write(' call fscomd_DoFSCommand(command, args)\n');
document.write('end sub\n');
document.write('</SCRIPT\> \n');
}
//-->
</SCRIPT>
<!-- URL's used in the movie-->
<A HREF=FSCommand:messagebox></A> <!-- text used in the movie-->
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
ID="fscomd" WIDTH="550" HEIGHT="400" ALIGN="">
<PARAM NAME=movie VALUE="fscomd.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src="fscomd.swf" quality=high bgcolor=#FFFFFF WIDTH="550" HEIGHT="400" swLiveConnect=true ID="fscomd" NAME="fscomd" ALIGN=""
TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
</BODY>
</HTML>

fscomd_DoFSCommand に以下のコードを追加します。

   function fscomd_DoFSCommand(command, args) {
    var fscomdObj = InternetExplorer ? fscomd : document.fscomd;
    //
    // Place your code here...
    //
    if (command == "messagebox") {
      alert(args);
    }
   }
これで、Flash上にあるボタンをクリックすると警告ダイアログが表示されるようになります。

ファイル更新日:2005年08月19日