ruby - facebook like button using unobtrusive js in rails -
i've put facebook button on rails app adding following tag html.erb page:
<div id="fb-root"></div> <script src="http://connect.facebook.net/en_us/all.js#appid=1419...&xfbml=1"></script><fb:like href="" send="" width="100" show_faces="" font="" layout="button_count" action="recommend"></fb:like>
and ... works ok big white space on page facebook sdk loading. so, thought i'd try putting in application.js file , running function after page loaded. problem can't work @ :)
so far, i've kept "fb-root" div in html , i've placed following function application.js
$(function() { $('#fb-root').after('<script src="http://connect.facebook.net/en_us/all.js#appid=1419...&xfbml=1"></script><fb:like href="" send="" width="100" show_faces="" font="" layout="button_count" action="recommend"></fb:like>'); });
however, not working me , can't figure out why (i'm js newb). appreciated. thanks!
you should load javascript function asynchronously:
<script> window.fbasyncinit = function() { fb.init({appid: 'your app id', status: true, cookie: true, xfbml: true}); }; (function() { var e = document.createelement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_us/all.js'; document.getelementbyid('fb-root').appendchild(e); }()); </script>
then once javascript library has loaded, convert fbml button appropriate html iframe code. if showing white space until loads, put button fbml code inside div , style div css.
here full example doesn't have issue talking (i load javascript library asynchronously, on timed delay simulate slow load. notice there no white while load button loading.
<!doctype html> <html> <body bgcolor="#000"> <div id="fb-root"></div> <fb:like href="http://stackoverflow.com" send="" width="100" layout="button_count" action="recommend"></fb:like> <script> window.fbasyncinit = function() { fb.init({appid: 'your app id', status: true, cookie: true, xfbml: true}); }; (function() { // delay simulate slow loading of facebook library - remove settimeout!! var t = settimeout(function() { alert('loading fb'); var e = document.createelement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_us/all.js'; document.getelementbyid('fb-root').appendchild(e); }, 3000); }()); </script> </body> </html>
Comments
Post a Comment