In early 2010, Facebook made some modifications to the API which made the embedding of iFrames more restricted. Although they are no longer supported in Static FBML tabs, iFrames can still be implemented on application Canvas Pages. However, rather than an iFrame loading automatically — as it previously did on Canvas Pages — a user action — a click — is now required to load the iFrame.
In a PowerPoint that Facebook circulated in late 2009, Facebook explained the upcoming changes:
iFrames
Preserving iFrames (on a custom tab)
If you currently use an iFrame in your page and must preserve the content of one or more of your iFrames: Create a wrapper application that lives on a canvas page. This application can have iFrames since it sits on a canvas page. Have this light weight application include the image of what you want to display, with an “Activate” button. Link the “Activate” button to the canvas page wrapper application. More information here.
And here is that “wrapper application,” written in JavaScript:
<a onClick="outside_location.setInnerFBML(link_1);" style="cursor: pointer;">Let's see that iframe....</a>
<div id="outside_location"></div>
<fb:js-string var="link_1">
<fb:iframe height="500" allowTransparency="true" frameborder="0" scrolling="no" style="width:100%; border:none" src="http://URL-TO-OUTSIDE-LOCATION"></fb:iframe>
</fb:js-string>
<script type="text/javascript" charset="utf-8">
var outside_location = document.getElementById('outside_location');
</script>
There are two flavors of Facebook applications — iFrame and FBML — depending on whether you will render your canvas page in an iFrame or with FBML. Obviously, to embed an iFrame you’ll want to create an “iFrame” application. There are a number of resources on the Web for creating a Facebook application and, actually, it’s really not that hard. Here are several:
- Developing a Facebook App for Absolute Beginners – Emmanuele Feronato
- Facebook Application Basics
- Facebook Developer Wiki – Creating a Platform Application
Once you have created your Facebook iFrame application, you can visit your application page, if you’re logged in to your Facebook account, by going to http://www.facebook.com/developers/apps.php where you will see your application listed in the left column.
Server Requirements for hosting a Facebook Application
- Make sure your Web server supports PHP, preferably PHP5;
- You’ll need FTP access to your server so you can create a directory and add files;
- You need to download the Official Facebook PHP 5 Client Library and upload it to your server (if your server has PHP 5 — if your server has PHP 4 instead of PHP 5, download the older Client Library). Click the “Download Source” button and select either “ZIP” or “TAR” compression option in the popup window:
After unpacking (unzipping) the file, install the folder “src” in a directory you create and name “facebook-platform” (not a required directory name, but a good choice) at the document-root level (eg “httpdocs” or “public_html” or “html” – the name varies) of your server (where the home page file is).
- More info is available on the Facebook Developer Wiki.
In the directory you create on your Web server for the “Canvas Callback URL” for your Facebook iFrame application, you’ll have two files:
- index.php – contains the content that will be rendered in the app;
- appinclude.php – contains some meta info about the app.
For your “index.php” file, you’ll need to render the “wrapper application” referenced above in PHP:
<?php
// File: 'index.php'
require_once 'appinclude.php';
echo '<a onClick="outside_location.setInnerFBML(link_1);" style="cursor: pointer;">Let\'s see that iframe....</a>';
echo '<div id="outside_location"></div>';
echo '<fb:js-string var="link_1">';
echo '<iframe height="500" allowTransparency="true" frameborder="0" scrolling="no" style="width:100%; border:none" src="[ABSOLUTE URL TO iFRAME CONTENT]">';
echo '</iframe>';
echo '</fb:js-string>';
echo '<script type="text/javascript" charset="utf-8">
var outside_location = document.getElementById("outside_location");
</script>';
?>
For the “appinclude.php”:
<?php
require_once ($_SERVER["DOCUMENT_ROOT"] . '/facebook-platform/src/facebook.php');
// *** Add your Facebook API Key, Secret Key here ***
$appapikey = '[APPLICATION API KEY]';
$appsecret = '[APPLICATION SECRET KEY]';
$facebook = new Facebook($appapikey, $appsecret);
// $user_id = $facebook->require_login();
?>
Regarding the above, replace “[APPLICATION API KEY]” and “[APPLICATION SECRET KEY]” with the ones generated when you create your application, and make sure your have the correct URL to the “facebook.php” file.
The “$user_id” is commented out (with the “//” preceding it) because it will cause the application not to function.
How to Hide the Activation Image or Text After User Clicks
In order to hide the activation image or text once it’s been clicked, you need to add a bit of FBML to the href, the “clicktohide” parameter. See below for how to implement this…
<?php
//
require_once 'appinclude.php';
echo '<div id="iframeLinkDiv"><a clicktohide="iframeLinkDiv" onClick="outside_location.setInnerFBML(link_1);" style="cursor: pointer;"><img src="ABSOLUTE URL OF IMAGE" border="0" alt="Photobucket" /></a></div>';
echo '<div id="outside_location"></div>';
echo '<fb:js-string var="link_1">';
echo '<iframe height="660px" allowTransparency="true" frameborder="0" scrolling="yes" style="width:760px; border:none" src="ABSOLUTE URL OF IFRAMED PAGE">';
echo '</iframe>';
echo '</fb:js-string>';
echo '<script type="text/javascript" charset="utf-8">
var outside_location = document.getElementById("outside_location");
</script>';
?>
External Resources
- Facebook Application Basics
- Facebook Developer Wiki – Creating a Platform Application
- Facebook Developer Wiki – Choosing between an FBML or iFrame Application
- Facebook Developer Wiki – iFrame Applications
- Developing a Facebook App for Absolute Beginners – Emmanuele Feronato
- Tutorial – Bring It On Home
I can only offer very limited support for Facebook app development. But if you post questions here, others may be able to help out.




#1 by sf - March 9th, 2010 at 11:57
What if I don’t have an ‘appinclude.php’ file on my server… Where does that come from? Sorry I’m kind of a beginner.
For my index.php file am I good? I have the following :
[Code deleted...]
Thanks for your help!
#2 by admin - March 9th, 2010 at 17:18
As I mentioned, I don’t have time to troubleshoot everyone’s code, particularly their PHP.
The appinclude.php is a file you must create and have on a server.
#3 by Andreas Sefzig - April 6th, 2010 at 11:44
You will have to create the appinclude.php yourself, it is not a given in any server’s preconfig (or by the sdk I believe you have gotten used to). Use the ‘template’ Tim has provided and fill in the data presented to you by facebooks developer-app. The rest looks fine to me (but I haven’t tried it). Good luck!
#4 by rich - March 10th, 2010 at 13:55
quicky.. does the app need to be iframe or fbml?
#5 by admin - March 10th, 2010 at 14:13
iFrame.
#6 by rich - March 10th, 2010 at 21:59
got it working, thanks. got a tip to “smartsize” or auto-height the iframe?
#7 by Justin Meighan - March 25th, 2010 at 07:21
Does the application setting matter? (iframe or fbml)
From what I gather, when you add an application as a tab, it is rendered as FBML anyway (as I found out the hard way with my iframe app)
The application tab is really very restrictive, as its more of an application view that a full on application in a tab.
This is a great post and could provide a workaround to simply pull your app in via the canvas url and have it all sitting pretty in a profile tab.
Thanks for sharing.
#8 by timware - March 25th, 2010 at 07:44
I’m not sure if you can use iFrames in an FBML canvas page, as opposed to an iFrame canvas page. I know you can in the iFrame canvas page because I did it.
But, yeah, with the application tab width soon to be reduced to 520px, the canvas page is the way to go, as it’ll still be the 760px width, and allows much more functionality.
#9 by Andreas Sefzig - April 6th, 2010 at 12:04
I believe in terms of usablilty tabs have the valuable advantage to integrate gracefully into the features fanpages are offering, like wall, info, notes; while canvas-pages of apps pop out of the ‘usual’ user-experience facebook-users are used to.
On a canvas-page, facebook-newbies might not find their way to the rest of your offerings and tend to use the back-button of their browser – these people are lost possibilities… (You could solve this issue by providing links, still people will not find them intuitively.)
Application tabs can be (almost) as feature-rich as canvas-pages. I would (and do) recommend fanpage-tabs, even when they are smaller, to have all the great content your brand provides showcased just one click away.
#10 by Nick - June 24th, 2010 at 12:46
Just following back up on this, as it looks like this width change should be coming late July (right when my go live is scheduled). If we follow the canvas page method outlined here, are we exempt from the 520px width? I have made my application at 760 and everything is good to go now. The primary route to my application will be from a tab on a brand fan page. It will be viewed under a tab, but your comment above seems to imply it’s not the same type of application. Do I need to reformat everything to 520?
Perhaps I’m confusing application tabs with fan page tabs? My application has no tabs, just iframed content.
#11 by timware - June 24th, 2010 at 20:04
Canvas apps are still allowed to be 760px.
#12 by Nick - June 28th, 2010 at 08:15
Great, so to clarify – this is considered a canvas app. Even though it’s presented via a tab on a fan page, it’s not truly considered an application tab – and therefore is not restricted to 520px. Just want to make sure I’m on the same page. Thanks!
#13 by Andreas Sefzig - April 6th, 2010 at 11:36
You can definitely use FBML too.
In some (some!) scenarios this even is the better option: You can render any site through the app’s fbml-interpreter, if you wish. Downside is, the app will throw errors for any not-supported tag. Upside is, you can implement fbml-functionality in your usual site (i.e. your blog) which will only show up if you render it through facebook. This might be great opportunity if you use Facebook Connect and empowers you to add facebook-specific features to your existing codebase. But be careful, there are many unsupported html-tags that can make facebook trip and fall…
#14 by Derek - March 29th, 2010 at 16:51
I believe I followed all the steps correctly, however, I’m getting the following error:
Fatal error: Call to undefined function phprequire_once() in /home2/blah/public_html/websiterootfolder/US-page/appinclude.php on line 1#15 by admin - March 30th, 2010 at 07:14
Sorry Derek. I can’t troubleshoot PHP issues. I’d hit the forums: http://www.facebook.com/developers/
#16 by Gregory Edgerton - June 10th, 2010 at 06:21
I think I had the same error in my code which I was able to correct. Check the source for your src/facebook.php file. It should be in a folder in the root location. I originally used a absolute URL for this file and it didn’t work. Once I removed the root it did.
<?phprequire_once ($_SERVER["DOCUMENT_ROOT"] . '/facebook-platform/src/facebook.php');
#17 by timware - June 10th, 2010 at 06:36
Ah yes, if you have the ["DOCUMENT_ROOT"] in there that takes care of “http://www.yourdomain.com”, so having that bit in the URL that follows wouldn’t work at all, as it’d be redundant. Thanks!
#18 by Raza - April 8th, 2010 at 12:35
Hello,
thanks for your wonderful post! i used your method and its working great!
i want to know how can i add a facebook application to my facebook fan page tab? or a direct site integrated on facebook page? Please Help!
thanks
#19 by elba - April 11th, 2010 at 10:52
Hi,
Thanks for the post. It was a big help. Has somebody a tip to how to remove the “Activate” link after the user clicked on it? It is kind of annoying after the iframe has loaded.
Thanks in advance.
#20 by timware - April 11th, 2010 at 11:33
Your solution is right in this post! Please read the entire post before posting a question. Thanks!
#21 by one7 - April 30th, 2010 at 16:56
guess i missed it too..can we get a one word hint…
#22 by timware - May 1st, 2010 at 17:58
Hah! Well, to my embarrassment I hadn’t included this bit of information. So take a look in the post now. It’s there, in all its splendor
#23 by arena - April 14th, 2010 at 02:05
Hi, Timware
nice post.
My app works great. here is the Canvas URL : http://apps.facebook.com/test_mailpress/
However i am still struggling to get this app (an iframe with a subscription form doing ajax with my own website) into a widget i could include in a Fan Page or best in any fb page.
Thanks for your help and your time
#24 by timware - May 1st, 2010 at 17:41
If you’re using an iframe in your app, you need to have an activation image or text that the user clicks before the iframe loads. If you don’t have the activation image/text for triggering the iframe (and how to do this is explained in the post), then your iframe will load on your application page, but it won’t work when it’s pulled in as a tab application.
#25 by arena - May 11th, 2010 at 06:26
thank you timware to confirm what i read.
btw i am wondering how these guys can have an ajax form on their fb wall (or is it a fake wall page ?) ?
any idea ? would be nice to have your expertise/feedback.
http://www.facebook.com/cluster.efectos?v=wall
#26 by timware - May 11th, 2010 at 08:32
If you’re talking about the yellow form in the left column box, it appears to be a valid form. I didn’t test it. But all the left-column box stuff is going away soon anyway. Currently you can’t add anything to the left column boxes.
#27 by nicos - May 3rd, 2010 at 09:22
Thank you very much for the tip ! works great, my boss is happy and me too… even if the ‘click to activate’ is not an ideal solution, it works !
#28 by Jeanne - May 6th, 2010 at 10:02
in the appinclude script above, I see this:
// *** Add your Facebook API Key, Secret Key, and Callback URL here ***
So I see where to add the app key adn secret key. Does the callback url go next to “$facebook = “? like this:
<?php
require_once ($_SERVER["DOCUMENT_ROOT"] . '/facebook-platform/php/facebook.php');
// *** Add your Facebook API Key, Secret Key, and Callback URL here ***
$appapikey = '[DELETED]';
$appsecret = '[DELETED]';
$facebook = 'http://www.mycallbackurl.com($appapikey, $appsecret)';
// $user_id = $facebook->require_login();
?>
Also, I keep getting errors regarding this line:
require_once ($_SERVER["DOCUMENT_ROOT"] . '/facebook-platform/php/facebook.php');– I have pasted that *as is* into my file. Was I supposed to change any data in this line of code?Thanks!!
#29 by timware - May 6th, 2010 at 13:47
Jeanne, Thanks for this comment. I needed to update this post in light of the new Facebook API and because I omitted an important piece of information — downloading the Facebook PHP 5 (or PHP 4) Client library and installing it on your server. This is where the “require_once” in appinclude.php points and it’s essential that it’s present on your server. Sorry for that! And, for the “$facebook =” part, just use what I have in my example rather than the “mycallbackurl.com” that you have.
#30 by Nick - May 20th, 2010 at 14:51
Fantastic tutorial, thanks! I have my test application working if I browse to the url directly – how do I go about adding a tab to my new app on my fan page? Also, strangely – my iframe content seems to render immediately, not requiring the click to activate. Did anything change recently in FB policy that would explain this?
#31 by timware - May 20th, 2010 at 18:06
You need to add the app to a fan page. Then it’ll show up as a tab. When you’re viewing the app directly rather than via a fan page, the iframe will load without a user action. Once it’s a tab on a fan page, then it won’t load automatically.
#32 by Esteve - June 3rd, 2010 at 09:06
Amazing post. And I’m sorry is this is too basic but I’m struggling. I’ve developed an app and it is working fine, and I want the app to be used from a fan page (in a tab). So I realized I need to provide a link to a page inside Profiles section an a tab URL.
What is this TAB URL useful for? Do I need to rewrite my application to be only in FBML (even though I’m pretty sure it is not possible?)? The way facebook has thought it’s to rewrite the application once for a canvas page and another for profile page?
Thanks in advance,
Esteve.
#33 by timware - June 6th, 2010 at 18:09
The Tab URL is where Facebook will pull the content for your iFrame Canvas App. You should first fill in the Canvas Page URL (under the Canvas tab). Then fill in the URL under “Tab URL” — I always use “index.php” in the URL. As it says: “Facebook pulls content for your tab from this URL, which must be relative to your canvas page.” Here’s a good resource for creating canvas apps: http://forum.developers.facebook.com/sandbox/viewtopic.php?id=9074
#34 by timware - June 28th, 2010 at 09:09
Nick, It is my understanding that canvas applications are still allowed to be 760px in width after the application-tab width is shrunk to 520px. Canvas apps can, and usually are, presented on the page as tabs.
#35 by Nick - June 29th, 2010 at 11:57
great, thanks!!
#36 by Mathias - July 7th, 2010 at 01:57
I found a little bug in the Iframe script when implementing it on a static FMBL page with a visible to connect tag.
The code doesn’t work if the screen is refreshed.
Javascript error something like link_1 doesn’t exist.
I tried moving the link part above the clickcode but it doesn’t seem to help.
Any idea how to fix this?
#37 by timware - July 9th, 2010 at 11:41
All I can say is that you CAN’T use iFrames in Static FBML, only in iframe canvas applications.
#38 by Peter from; Miami Web Design - July 14th, 2010 at 09:08
Very interesting since I had no idea you could add iframes to facebook.
#39 by Nick - July 14th, 2010 at 10:00
Hey Tim – Seeing a strange issue, wondering if you’ve come accross it. In my app I have a gradient bkd image on the body tag that lays on top of a solid html bgcolor. In FF, mac only, when viewing the application through the FB iframe only, when I click on any of my links, for a split second, only on mouse click, the block area of the a tag loses any background, and shows a quick block of the solid html body color. That same code when browsed directly in FF/mac is fine. It is fine in all other browsers both browsed directly, and through the FB app. Have you come across anything similar in your experiences?
#40 by Nick - July 14th, 2010 at 10:02
Actually – it’s not on click, but on release, or mouseup.
#41 by Chuck - July 19th, 2010 at 15:12
Question – my app is running on a profile tab with no problems EXCEPT it won’t allow the share to newsfeed/post to wall popup to appear. I’ve seen this happen with other applications (see the Alice in Wonderland official app). Any ideas if there’s a workaround?