ExteralInterface Tutorial: Calling Fancybox from Flash
7 Apr
So, over the past few days, I’ve been working on a project that requires a fancybox call from flash. WOE. I know, say what? It’s actually a lot easier than you think! So, Check this out… here is how to call fancybox from your AS3 swf.
After the directions… you’ll notice I trying to call a list of images (a gallery). If anyone has achieved that, please feel free to let us all know.

1. create a button in AS3 (or movieclip)
2. give it an instance name of myButton.
3. In order for fancybox to hear your click, you’ll have to use ExternalInterface to call your js functions from flash.
Sounds like a mouth full, but it’s simple. Just add this to the first frame of your
time line:
// IMPORT CLASSES import flash.external.*; // ADD MOUSE EVENT LISTENER myButton.addEventListener(MouseEvent.CLICK, myClick); // CREATE FUNCTION function myClick(e:MouseEvent):void { // CALL YOUR JS FUNCTION HERE IN STRING FORM ExternalInterface.call("callFancy"); }
4. Now we need to add some code to our HTML document. First, we need
to add a hidden < div > container. So, just above your flash div,
(assuming your using SWFObject — which you should be) add this:
<div id="hidden_clicker" style="display:none;">
<a class="featured"></a>
</div>
****NOTE : You must call your swfobject.js first… then put in the swf calls, followed by the jquery.min.js, followed by all your fancybox.js scripts. See example for more info ****
5. Now we need to add our JavaScript code. You’ll see the name of
this function is the same as the string we called in flash. Add the
following to your < head > section:
<script type="text/javascript" > function callFancy(my_href) { var j1 = document.getElementsByClassName("featured"); j1.href = my_href; $('.featured').trigger('click'); } </script>
6. OK, now that we are done with that, we still need to define our
fancybox JavaScript — to make it look the way I want… now, this is where I
have questions, but I’ll get to that in a minute. Add this to your
fancybox call. In my case ‘.featured’.
**NOTE – I am calling several different fancybox calls, so I have a
separate .js file with all these calls in it. make sense?
$(".featured").fancybox({ 'overlayOpacity' : 0.7, 'overlayColor' : '#000', 'centerOnScroll' : true, 'hideOnContentClick' : true, 'hideOnOverlayClick' : true, 'autoDimensions' : true, 'padding' : 0, 'href' : 'PUTYOURIMAGEHERE.jpg', 'title' : 'PUTYOURTITLEHERE', 'transitionIn' : 'fade', 'transitionOut' : 'fade' })
7. That’s it.. your done!
Get the SOURCE files here
I hope you find this helpful.
L
Popularity: 100%
Related posts:
- Using Fancybox for a PopUp I know, I...
- Video Tutorial :: GAIA Framework Part 1 I’ve had the...
- Friday Video of The Day and Flash Iphone Video Tutorial I’ve really been...
Thanks for this great post, Im having a hard time getting it to work with IE 8 – any ideas on what i can change or add to make it work with IE? Thanks Again
I haven’t checked it in IE — i’ll do a full browser check later this afternoon.. and let ya know what’s going on.
Seems like it would have something to do with SWFObjoct… or maybe the CSS. (just off the top of my head)
Thanks Zunski, I appreciate your help
This is epic, but I have made a custom gallery using flash, and this only appears to work for 1 element. I need to make it work for approximately 18 elements. Do you have any ideas or work arounds that you have already implemented?
@Sino – still havent got to the IE thing.. I will be testing this tomorrow though.. just about to that stage on my current project.
@Incredulous – yeah, thats simple… just create different instances of ExternalInterface.call
Like so…
then, in your “hiddenClicker” div…
then in your .js….
make sense? now. there are plenty of ways to combine each section of your code, but I don’t have time to get into all that… It’s opening day here in St. Louis… GO REDBIRDS!
Essentially you just have to create different instances of the ExternalInterface.call function. and duplicate that over the course of however many times you need a seperate fancybox window click in flash. I hace a gallery as well… and each photo has a different “EI.call” function.
Simple.
Hope that helps.
& BTW — I just changed my WP theme… I like this better..for now.
EDIT — Just realized its tailgating time.. gotta jet.. so I’m putting the old theme back.. no time to spice this one up.
@ zunski, I figured it out that work around about 30 minutes after I asked you, but I hoped there was an easier way! Thanks though!
Have to be honest here. I haven’t written AS2 in years. So i wont be of much help in that dept.
But if I were you, I’d search adobe labs. When I was researching this I found examples of externalinterface in both AS2 and AS3. Hope that helps.
Lance
I’m sure there might be an easier faster method. Possibly using a for loop?
Call your js parameter from flash like so:
(then you can pass strings from xml, etc.)
@Sino – I am still trying to work the IE thing out. It’s actually a pain the A double S.
I seems that I can’t call any Javascript onClick event currently. I’m working on it.
If you or anyone else have found a solution, please let me know.
Thanks Zunski, for sure, I am also trying to figure it out, If I do, I will def be back to post it. Thanks Again
@Sino –
Ok. I figured out the IE thing. VERY FRUSTRATING. But very simple solution.
1. You should be using swfobject 2. It’s the defacto way to embedd your flash into webpages.
2. You need to give your swfobject a “UNIQUE” id in order for IE to register what your trying to do.
Here is my swfobject for a working example at woodandahammer.com :
Notice the attributes lines. You MUST add those 2 lines to get IE to recognize this. Also, remember, it needs to be unique. Call it pancakes if you have too.
Hope this helps!
L
wow, thanks lance for sharing, Im going to give it a shot, thats a sweet site by the way
Hi,
I try the Lance’s solution, but it doesn’t work on IE 6+
I dont understand.
Width this method : http://outburst.jloop.com/2009/08/06/call-fancybox-from-flash/
its work on firefox and IE6+ but on IE6+, you just see the link’s contain (= “Hidden Clicker”).. so i don’t understand.
Somebody can help me ? can you show an exemple on internet please ?
thx
I forgot my example :
http://gargogne.free.fr/sites/carsemotions/liens_vehicule_collection.php
the link (flash) open a div (div id=”#mariage”) with fancybox.
I have in my flash link :
and in my fancy function :
thx !!
Great article, thank you very much!
I find the solution :
its work on IE6+
Fancybox from flash. All browsers (end Internet Explorer).
//HTML.
EI Tutorial
<a href="#" rel="nofollow"></a> <a href="#" rel="nofollow"></a>Hi,
Was hoping someone could help me… We’re trying to implement this onto our site, but having a little trouble.
Firstly, when i used your swf, all works right, but when copying that into a new file it doesnt do anything. We then converted the movie clip button into an actual button and edited the code to call the external interface on release(including the import) but nothing
The other thing is, we are using it to embed an aspx form which makes a few calls to the server, whenever next is clicked(and a subsequent server call is made), the entire page crashes. Is there something i’m doing wrong?
Sorry i’m a serious newbie here…
oooooooooooh… got the button working… seems to be a P.E.B.C.A.K problem…
Still sitting with the aspx page issue though.
doesnt work in my localhost
flash file in source file cant opened, what kind flash version that was you use.
ps: sorry my englis is bad ^^;
@Lance
The IE issue with adding the attribute lines and making sure you have SWFObject2 still doesn’t seem to work for me.
Any other suggestions?