Flash and JavaScript Integration
Follow these easy steps to make flash communicate with JavaScript code and vice versa.
a) Flash to JavaScript: Making Flash call a JavaScript function
JavaScript Function Code. To be put in the HTML
<script language="JavaScript">
function alertMe(){
alert("Hello, world");
}
</script>
Flash Code. To be put in any event of button, frame, where ever required:
import flash.external.ExternalInterface;
function callJavaScript(){
ExternalInterface.call("alertMe");
}
Please note that due to default security these codes will not work if the flash is loaded locally.
To test this, you will either need to upload your .swf and .html to a server and request the files from the server.
OR you will need to set the global security settings( Google it to know how!)
b) JavaScript to Flash : Making JavaScript call a Flash Function.
Flash function code that needs to be called:
import flash.external.ExternalInterface;
function startPlaying(){
play();
}
ExternalInterface.addCallback("startPlaying", this, startPlaying);
Java Script code to call the method in the flash:
<script language="JavaScript">
var myflashref;
function getReference(){
if (navigator.appName.indexOf("Microsoft") != -1 )
myflashref = window.flashObject;
else
myflashref = window.document.flashObject;
}
function playFlash(){
getReference(); //getting the flash reference.
myflashref.startPlaying(); //call the method in the flash
}
</script>
Well, I leave it up to you now to do some R&D and make data pass through flash and JavaScript and vice-versa!
Feel free to leave clarification requests.
Friday, April 13, 2007
Subscribe to:
Posts (Atom)