Javascript injection via as3 to embed external libraries

On my quest to make the facebook actionscript connectivity as easy to work with as possible I needed to be able to reproduce the


ExternalInterface.call(js, 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');

First let's look at the code to embed the call into the head.

			var head = document.getElementsByTagName('HEAD').item(0);
			script = document.createElement('script');
			script.src = src;
			script.type = 'text/javascript';
			head.appendChild(script);

This take sthe element on the html that is the head tag and creates a script element (pretty much just how it reads). the src is then pushed in through the call to ExternalInterface with

ExternalInterface.call(js, 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');

Once I create this initial test I ran the call and my flash kept disappearing. I was like WTF!?!?
Then after going through the Facebook js code I realized there was a test to check if there was a hidden div container called FB_HiddenContainer in the html and if not then document.write() it. This overwrote the embedding code I was using to embed my flash thereby removing it totally from the page. I use the same type of method for the insertion of the script tag to insert a div tag in the body of the html as follows:

				var body = document.getElementsByTagName('body').item(0);
				div = document.createElement('div');
				div.id = 'FB_HiddenContainer';
				body.appendChild(div);

And voila! You now have a [script src='http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'] making all FB methods available, as well as the [div id='FB_HiddenContainer'] container in the body to prevent that funkiness of removing your swf.

Hope this might be helpful to anyone wanting to do the same!


Posted

in

,

by

Comments

3 responses to “Javascript injection via as3 to embed external libraries”

  1. Russ Avatar
    Russ

    This seems all straight forward, but I just can’t get it to work with my script. I am trying to use SWFAddress, and point the script src to the swfaddress.js file on my server(the app lives on a different server), but i can’t get it to work. the only thing I changed with my code is removed all the references to the body and div addition, and removed the if(!window.FB)
    Any thoughts?

  2. Russ Avatar
    Russ

    just for clarification, viewing the page with Firebug, it seems to be adding the swfaddress.js file, but I can’t access any of the functions.

  3. Thaylin Avatar

    Are you trying to access swfAddress via your own methods? If so why? SwfAddress has a library for interacting with its js files that is perfectly good.

Leave a Reply