[펌] 플래시에서 자바스크립트 호출하기
1. html 페이지 Object/Embed 태그 안에 이름 설정
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/
flash/swflash.cab#version=5,0,0,0"
width=366 height=142 id="myFlash">
이와 같이, Object일 경우 id를 설정해 준다.
그리고, Embed 태그는 다음과 같이.
<embed src="javascript_to_flash.swf" quality=high width=366 height=142
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"
name="myFlash" swLiveConnect="true">
</embed>
이름과 swLiveConnect를 true를 해준다.
2. 다음의 스크립트를 html에 박아 넣는다..
<SCRIPT LANGUAGE=JavaScript>
<!--
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
function myFlash_DoFSCommand(command, args) {
var myFlashObj = InternetExplorer ? myFlash : document.myFlash;
alert (args);
}
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 myFlash_FSCommand(ByVal command, ByVal args)\n');
document.write(' call myFlash_DoFSCommand(command, args)\n');
document.write('end sub\n');
document.write('</SCRIPT\> \n');
}
//-->
</SCRIPT>
3. 이제, 플래시에서 다음의 명령으로 자바스크립트를 호출할수 있다.
fscommand ("send_var", "hello world");
4.한가지 주의할 사항은
fscommand ("send_var", "hello \nworld");
와 같이
개행문자등 특수 문자가 들어가 있으면 자바스크립트 호출시 에러가 나는것 같다.
이럴경우 escape과 같은 함수로 파라미터를 인코딩해서 보내면 된다.
즉
fscommand ("send_var", escape("hello \nworld"));
그리고, 자바스크립트에서는 unescape으로 디코딩한다.
alert (undescape(args));
참고
샘플페이지: http://www.macromedia.com/support/flash/ts/documents/java_script_comm.htm