;

PDA

Click to See Complete Forum and Search --> : JS to SWF


MoisesZaragoza
11-04-2008, 05:00 PM
Hi,
I have been having some problems passing information from JS to a SWF
What i want to so is when i press a BTN in my HTML Page that it calles a JS Funcrion and calls a function inside flash.

any ideas how to do this?

my SWF is really simple i just have 1 text field called myText and on the action leyer i have



pickTest = function(){
myText.text = "123"
}




HTML HEADER





<script language="javascript" type="text/javascript">
function getText(returnText){
alert(returnText)

if (navigator.appName.indexOf("Microsoft") != -1){
window["spell_check"].pickTest();
}else {
document["spell_check"].pickTest();
}
}
</script>




BODY




<script language="javascript">
if (AC_FL_RunContent == 0) {
alert("This page requires AC_RunActiveContent.js.");
} else {
AC_FL_RunContent(
'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
'width', '550',
'height', '400',
'src', 'spell_check',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
'play', 'true',
'loop', 'true',
'scale', 'showall',
'wmode', 'window',
'devicefont', 'false',
'id', 'spell_check',
'bgcolor', '#ffffff',
'name', 'spell_check',
'menu', 'true',
'allowFullScreen', 'false',
'allowScriptAccess','sameDomain',
'movie', 'spell_check',
'salign', ''
); //end AC code
}
</script>
<noscript>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="spell_check" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="spell_check.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /> <embed src="spell_check.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="spell_check" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</noscript>


<input type="button" value="OpenNewWindow" onclick="getText(123)" />




Thanks for all the help

5TonsOfFlax
11-04-2008, 05:16 PM
You need to use ExternalInterface, and specifically the addCallback method to register pickTest as a function callable from javascript. You should also change the way you're defining pickTest.




function pickTest():void{
myText.text = "123"
}

ExternalInterface.addCallback("pickTest", pickTest);


Edit: It probably doesn't fit your needs exactly since it was written for class based rather than frame based code, but I wrote up a utility to help out with some ExternalInterface stuff. http://cosmodro.me/blog/2008/aug/31/expose-your-tool/

As always with ExternalInterface, it must be on an actual webserver to work. Testing on the local filesystem doesn't work.

MoisesZaragoza
11-04-2008, 05:44 PM
so if i am in localhost this will not work?

5TonsOfFlax
11-04-2008, 05:46 PM
localhost should work, so long as you're serving it from an actual webserver. Does your url start with http:// or file://? The former will work, the latter won't.

MoisesZaragoza
11-04-2008, 05:47 PM
i moved the code to a testing server
http://www.wilopenwindows.com/_test/FlashJS/Default.aspx
but i am still getting a error

window.spell_check is null or not a object

5TonsOfFlax
11-04-2008, 05:52 PM
Your object/embed needs to have an id. And your crossbrowser stuff doesn't work for firefox. It should be something like

document.getElementById("spell_check").pickTest();


Look into swfobject, which makes embedding flash a lot easier than the AC_FL_RunContent crap that adobe publishes.

MoisesZaragoza
11-04-2008, 05:55 PM
i just updated my code to use

document.getElementById("spell_check").pickTest();


but i am getting a new error that reads

Object does not support this property ot method



and BTW thanks you so much 4 helping me out

5TonsOfFlax
11-04-2008, 05:58 PM
It's a two part fix. First you have to fix the embedding code so that it does put an id on the thing. Then you need to access it correctly.

Which browser are you using?

illustratedlife
11-05-2008, 02:05 AM
FWIW, i'm using moofx's swiff class to link flash and js tweens. i can run it from file:/// just fine

5TonsOfFlax
11-05-2008, 02:13 AM
Interesting. I'm assuming that still uses ExternalInterface for the js->flash communication. I wonder if Adobe fixed that thorn in my side with the 10 player. There was no good reason for it to fail on file:// pages in the first place.

Can you provide some links to moofx swiff? I'm having a bit of trouble finding good documentation online.

MoisesZaragoza
11-05-2008, 09:15 AM
this is my new flash code


import flash.external.*;

function pickTest() {
myText.text = "123";
}

ExternalInterface.addCallback("pickTest", this, pickTest);

5TonsOfFlax
11-05-2008, 09:54 AM
ExternalInterface.addCallback only takes 2 arguments. It shouldn't even compile the way you have it there. Remove the "this" argument.