javascript and flash communication problem with dynamically inserted flash object

385 views Asked by At

I was working with javascript and flash communication with AS2 using ExternalInterface. This works well with my flash game being embedded in a dummy page and calls from and to flash/javascript were working fine without any errors.

In my new page, I am dynamically inserting the flash object in a div (by jquery). The problem is that when I call the flash function like flash_Obj.JS2Flash_SetValue(val); it says that JS2Flash_SetValue is not a defined function. Although when I check for the presence of the object/embed element, it does show the element (i used firebug to debug).

Otherwise, when in a dummy page, I was testing javascript and flash communication, it ran without any errors.

I have looked in to this question - Javascript loses communication with flash when moved inside dom

but it wasn't much of help.

Can someone please suggest a solution?

flash code

ExternalInterface.addCallback("JS2Flash_SetValue", null, SetValue);
var fVal:String;
function SetValue(_val:String):Void
{       
    ExternalInterface.call("ShowAlert", _val + " has received from JS.");
}

JS code

function SetValue1(val) // first approach i tried
 {          
  var flash_Obj = $('#flashGame').[0];
  flash_Obj.JS2Flash_SetValue(val);

  var flash_Obj1 = $('#flashGame').get(0);
  flash_Obj1.JS2Flash_SetValue(val);
 }

 function SetValue2(val) // another approach i tried
 {           
   var flash_Obj = getMovieName('flashGame');    
   flash_Obj.JS2Flash_SetValue(val);
 }

 function getMovieName(movieName) {
   if (navigator.appName.indexOf("Microsoft") != -1) {
       return window[movieName];
   }
   else {
       return document[movieName];
   }
 }

 function ShowAlert(msg) {
   alert(msg);
 }

Thanks

1

There are 1 answers

0
saarthak On BEST ANSWER

simple it was. used document.getElementById('flashgame').JS2Flash_SetValue(msg) and it worked like a charm