[CS3|AS2] Calling ActionScript functions from JavaScript

Problem with the ExternalInterface.addCallBack method

Page 1 of 1

3 Replies - 6196 Views - Last Post: 16 March 2008 - 09:14 AM

#1 lost child  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 01-March 08

[CS3|AS2] Calling ActionScript functions from JavaScript

Post icon  Posted 13 March 2008 - 07:45 PM

'Sup, guys?
I've been embedding my Flash movies into HTML using deconcept's swfobject.js script ever since I learned about its existence.
Anyway, I looked carefully at this example but couldn't apply it to my case. Could you please tell me what I'm doing wrong? I simplified the task to just an HTML button calling a Flash function:

Here's my ActionScript2 Code from eitest.swf
____________________________________________________
import flash.external.ExternalInterface;
this.createTextField("myTextField", 1, 0, 0, 200, 30);
function changeMyTextFieldText()
_____{
__________myTextField.text = "!!!SUCCESS!!!";
_____}
ExternalInterface.addCallback("changeMyTextFieldTextFuncRegID", null, changeMyTextFieldText);

____________________________________________________
Here's my HTML code from eitest.html
<script type="text/javascript" src="swfobject.js"></script>		
<div id="flashcontent">
  This text is replaced by the Flash movie.
</div>
<script type="text/javascript">
   var so = new SWFObject("eitest.swf", "eitestID", "200", "30", "9", "#FFFFFF");
   so.write("flashcontent");   
   function callASfunction()
   	{
	  calleFlashMovie = document.getElementById("eitestID");
	  calleFlashMovie.changeMyTextFieldTextFuncRegID();
	}	
</script>
<input type="button" id="changetext" value="Call AS Function" onclick="callASfunction();"/>

Also, if it's not too much to ask, would you kindly show me a simple example with an argument:
function changeMyTextFieldText(textArg)
{
_____myTextField.text = textArg;
}


Is This A Good Question/Topic? 0
  • +

Replies To: [CS3|AS2] Calling ActionScript functions from JavaScript

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: [CS3|AS2] Calling ActionScript functions from JavaScript

Posted 14 March 2008 - 03:14 AM

Hi,

The example you've provided does actually work for me, here are some things you could check:
  • No other scripts on your page are interfering, try it on it's own first.
  • Your swf is published as version 8 or later.
  • You are running on a server, Flash security won't permit this type of interaction locally.

This code includes passing a string from Javascript to flash, your:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="swfobject.js"></script>
</head>

<body>
		
<div id="flashcontent">
  This text is replaced by the Flash movie.
</div>
<script type="text/javascript">
	var so = new SWFObject("eitest.swf", "eitestID", "200", "30", "9", "#FFFFFF");
	so.write("flashcontent"); 

	function callASfunction()
	{
	calleFlashMovie = document.getElementById("eitestID");
		//pass your desired string as an argument of this function
		//you can pass as many as you like
	calleFlashMovie.changeMyTextFieldTextFuncRegID("someString");
	}	
	</script>
<input type="button" id="changetext" value="Call AS Function" onclick="callASfunction();"/>

</body>
</html>



mport flash.external.ExternalInterface;
this.createTextField("myTextField", 1, 0, 0, 200, 30);
//recieve the parameters passed from Javascript as arguments of this function
function changeMyTextFieldText(textArg:String)
{
	myTextField.text = textArg;  //"!!!SUCCESS!!!";
}
ExternalInterface.addCallback("changeMyTextFieldTextFuncRegID", null, changeMyTextFieldText);



Hope that helps :)

This post has been edited by thehat: 14 March 2008 - 03:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 lost child  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 01-March 08

Re: [CS3|AS2] Calling ActionScript functions from JavaScript

Posted 15 March 2008 - 02:29 PM

Wow! Thank you so much, mate! I wasn't the only one looking for the answer, so it wasn't just me you SOSed.
The 3 possible culprits you listed were, in fact, in check. I could call JS from AS fine but, for some reason, I couldn't do the opposite. I had even tried setting the allowScriptAccess param to always, and still, I had no luck. It's funny you say my code worked for you because it still doesn't work for me. What's important, though, is that your code works like a charm and now I can look into it and try to find what I did so syntactically different that caused my code to fail.
Once again, thanks a bunch, Mr Hat Posted Image Problem solved! :bananaman:
Was This Post Helpful? 0
  • +
  • -

#4 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: [CS3|AS2] Calling ActionScript functions from JavaScript

Posted 16 March 2008 - 09:14 AM

Strange, I thought I just copied your code :blink:

Never mind though, if it works it works! Happy to help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1