I’m trying to use the ‘edge.create’ callback function of the Facebook javascript SDK to send an alert when a user clicks the like button on a page. Ultimately my goal is to have it regenerate styles on the page so that it can resize the facebook widget div dynamically when clicked. I cannot get the callback to work even for the alert though.
<html lang="en">
<head>
<title></title>
<script src="http://connect.facebook.net/en_US/all.js#appId=216985861663967&xfbml=1"></script>
<script>
FB.Event.subscribe('edge.create', function() {
alert('Liked');
});
</script>
</head>
<body>
<div id="fb-root"></div>
<fb:like send="false" layout="button_count" show_faces="false" font=""></fb:like>
</body>
</html>
Any ideas why this isn’t working? When the button is clicked it gives the error:
Unsafe JavaScript attempt to access frame with URL
and later says Domains, protocols and ports must match.
From your description I’m guessing you’re testing in Chrome/Safari on a local server?
Chrome/Safari
Try setting your Like Button URL to an existing Web address:
An important note is that the alert depends on the Like’s success, which depends on the URL being reachable by Facebook. You won’t be able to Like a URL that only exists on your local server (ex. http://localhost:3000/my-page.html). Obviously Liking Google is a test fix; you can instead push your code to a staging server for the same effect, but it takes more time and effort.
When I make this change the Like Button and alert work in Chrome 12 for Mac, but I still see the same error as you. I think Chrome is just sensitive with iframes; I doubt the error can be avoided.
Firefox
If I make the above change, it still doesn’t work in Firefox 5 for Mac. I get the error
e.root is undefinedfrom Facebook’s all.js and the Like Button doesn’t even appear.If I move the
all.jsandFB.Event.subscribe()scripts below thefb-rootdiv, the Like Button shows up and the alert works with no errors.Internet Explorer
According to Facebook’s documentation you need to add Facebook’s XML namespace to your
<html>tag for the button to render in Internet Explorer: