A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: ExternalInterface ...'is not a function'

  1. #1
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707

    ExternalInterface ...'is not a function'

    ok...i am at a loss now. I have googled this : externalinterface + "is not a function" and combed every forum.

    Using the external interface and swfobject, i need to pass variables from a select dropdown. Crazy as it is, i can capture from the flash to the js. In fact, upon initializing the movie , i capture an AS object from the flash to my js. My problem is passing new variables back. I always get "[functionName] is not a function" in my firebug.
    Code:
    //called from the swf to JS
    function varsFromFlash(ob)
    {
        alert('Flash is loaded an page is ready')
    }
    function getFlashMovieObject()
    { 
    	var movieName = "image_flash_content";
    	if(document.embeds[movieName])
    		return document.embeds[movieName];
    	if(window.document[movieName])
    		return window.document[movieName];
    	if(window[movieName])
    		return window[movieName];
    	if(document[movieName])
    		return document[movieName];
    	return null;
    }
    
    function loadNewXML()
    {
    	var tg = $("dd_track");
    	var xmlpath = tg.value;
    	var ob = new Object()
    	ob.xmlpath = xmlpath
    	sendFromJS(ob)
    }
    
    function sendFromJS(ob)
    {
        var mainmovie = getFlashMovieObject()
        mainmovie.sendVarsFromJS(ob);
    }
    and my super simple class
    Code:
    package com.website
    {
    	import flash.events.EventDispatcher;
    	import flash.external.ExternalInterface;
    	import flash.system.Security
    	public class FlashJS extends EventDispatcher
    	{
    		public var target:* = null;
    		public function FlashJS()
    		{
    			
    		}
    		
    		public function sendToJS(ob:Object):void
    		{
    			trace(this,"sendToJS(object)");
    			for(var b in ob)
    			{
    			//	trace(b,ob[b])
    			}
    			ExternalInterface.call('varsFromFlash',ob);
    		}
    		
    		public function getFromJS(ob:Object):void
    		{
    			if(target != null) ExternalInterface.addCallback("sendVarsFromJS",eventReceivedFromJS); 
    		}
    		
    		private function eventReceivedFromJS(v:Object):void
    		{
    			var ev:SSP_Event = new SSP_Event(SSP_Event.JS_EVENT,v)
    			target.dispatchEvent(ev);
    		}
    	}
    }

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You need to register your functions with the EI so it knows to port between the external (visible to js) function name and the internal function...JS can only see your swf as an object embedded in the page so it doesn't know anything about the internal structure, scope, etc.

    Try putting this into your constructor:

    PHP Code:
        ExternalInterface.addCallback('sendVarsFromJS'getFromJS); 

  3. #3
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    thanks neznein9, that get's rdi of the error, but still no functionality. Either there is more to it, or that does not do it. i have tried to put the addCallBack in both my constructor for the Document Class, and the FlashJS class itself. Both without luck. I even tried to let JS know we are good to go by call a function upon loading. The js is getting that function, but still can not send information back....any more ideaas?

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Hrmm...not sure what else to tell you. Throw in trace statements and alerts all over the place - I'd recommend starting with the JS targetting of the flash object and (if that's correct) the flashObject.function.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The actual function you need to expose is eventRecievedFromJS, and you need it exposed before the js tries to call it.

    Code:
    package com.website
    {
    	import flash.events.EventDispatcher;
    	import flash.external.ExternalInterface;
    	import flash.system.Security
    	public class FlashJS extends EventDispatcher
    	{
    		public var target:* = null;
    		public function FlashJS()
    		{
    		  ExternalInterface.addCallback("sendVarsFromJS",eventReceivedFromJS); 
    		}
    		
    		public function sendToJS(ob:Object):void
    		{
    			trace(this,"sendToJS(object)");
    			for(var b in ob)
    			{
    			//	trace(b,ob[b])
    			}
    			ExternalInterface.call('varsFromFlash',ob);
    		}
    		
    		private function eventReceivedFromJS(v:Object):void
    		{
                            trace("got stuff from js: "+v);
                            if (target != null){
     			  var ev:SSP_Event = new SSP_Event(SSP_Event.JS_EVENT,v);
    			  target.dispatchEvent(ev);
                            }
    		}
    	}
    }

  6. #6
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    i can see i will have to rethink my structure. The only way i can get EI to respond is to actually put it on the fla frame 1. That seems way ,WAY wrong. I can not have a class manage EI?

    i ran traces and alerts. When the swf loads, it calls to JS, js alerts the message, and send back to flash a stupid string. And just to be sure it's getting it, i asked flash to call back one more time with a confirmation...which never came back. As well i track everything in Terminal so i can read the flash traces live. Nothing on the JS going in unless i put the EI on the first frame.

    i only have one frame in the movie, and all my code is in classes, so i do not want to have to resort to putting even the simplest thing on the timeline.

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    There should be no need to put code on the timeline rather than in a class. I'm not sure where you're going wrong, but don't start from scratch just yet.

    Check out these examples on my blog for working with ExternalInterface. Both show two way communication.

    http://cosmodro.me/blog/2008/sep/1/h...-part-i-xeyes/

    http://cosmodro.me/blog/2008/aug/31/expose-your-tool/

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Change your javascript to this:

    function sendVarsFromJS(ob)
    {
    var mainmovie = getFlashMovieObject()
    mainmovie.sendVarsFromJS(ob);
    }

    and change to this:

    private function eventReceivedFromJS(ob:Object):void
    Last edited by cancerinform; 03-25-2009 at 10:10 AM.
    - The right of the People to create Flash movies shall not be infringed. -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center