{"id":2514,"date":"2016-10-29T15:14:36","date_gmt":"2016-10-29T14:14:36","guid":{"rendered":"http:\/\/www.nivas.hr\/blog\/?p=2514"},"modified":"2016-10-29T15:14:36","modified_gmt":"2016-10-29T14:14:36","slug":"proper-way-include-facebook-sdk-javascript-jquery","status":"publish","type":"post","link":"https:\/\/www.nivas.hr\/blog\/2016\/10\/29\/proper-way-include-facebook-sdk-javascript-jquery\/","title":{"rendered":"Proper way to include Facebook SDK for Javascript and jQuery &#8230;"},"content":{"rendered":"<h2>&#8230; and avoid race condition! :)<\/h2>\n<p>Although fb is pretty clear on how to do it (<a href=\"http:\/\/bit.ly\/2eGfFDv\" target=\"_blank\">http:\/\/bit.ly\/2eGfFDv<\/a>)  \u2013 many developers are still doing it wrong.  So this blog post is here to be reminder for me and others, also it will propose small upgrade to the fb recommended practice.<\/p>\n<h2>The naive and wrong way to do it<\/h2>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script src=&quot;\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.0.0\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nwindow.fbAsyncInit = function()\r\n\t{\r\n\t\tFB.init({\r\n\t\t\tappId      : 'your-app-id',\r\n\t\t\txfbml      : true,\r\n\t\t\tversion    : 'v2.8'\r\n\t\t\t});\r\n\t\t\r\n\t\t\/\/ do something with DOM and jQuery\r\n\t\t$('#loginbutton,#feedbutton').removeAttr('disabled');\r\n\t\tFB.getLoginStatus(updateStatusCallback);\r\n\t};\r\n\r\n  (function(d, s, id){\r\n     var js, fjs = d.getElementsByTagName(s)[0];\r\n     if (d.getElementById(id)) {return;}\r\n     js = d.createElement(s); js.id = id;\r\n     js.src = &quot;\/\/connect.facebook.net\/en_US\/sdk.js&quot;;\r\n     fjs.parentNode.insertBefore(js, fjs);\r\n   }(document, 'script', 'facebook-jssdk'));\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>The problem with this is the race condition: at the time fbAsyncInit gets called there is no guaranty  that  DOM is ready and that #loginbutton or #feedbutton is there, ready to be used.<\/p>\n<p>This bug is hard to spot and debug \u2013 since it will not always manifest. It is very likely that you as developer with fast connection and computer will never  experience this bug \u2013 but your clients (visitors) might. <\/p>\n<p><img loading=\"lazy\" src=\"http:\/\/www.nivas.hr\/blog\/wp-content\/uploads\/2016\/10\/meme-450x338.jpg\" alt=\"meme\" width=\"450\" height=\"338\" class=\"aligncenter size-medium wp-image-2515\" srcset=\"https:\/\/www.nivas.hr\/blog\/wp-content\/uploads\/2016\/10\/meme-450x338.jpg 450w, https:\/\/www.nivas.hr\/blog\/wp-content\/uploads\/2016\/10\/meme.jpg 604w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>So it is best not to leave this to chance since fix is so easy.<\/p>\n<h2>Facebook recommendation<\/h2>\n<p>Learn more: <a href=\"http:\/\/bit.ly\/2eGfFDv\" target=\"_blank\">http:\/\/bit.ly\/2eGfFDv<\/a><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script src=&quot;\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.0.0\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n$(document).ready(function()\r\n{\r\n\t$.ajaxSetup({ cache: true });\r\n\t\r\n\t$.getScript('\/\/connect.facebook.net\/en_US\/sdk.js',\r\n\t\t\t\tfunction()\r\n\t\t\t\t{\r\n\t\t\t\t\tFB.init({\r\n\t\t\t\t\tappId: '{your-app-id}',\r\n\t\t\t\t\tversion: 'v2.8'\r\n\t\t\t\t}\r\n\t\t\t);\r\n\r\n\t\t$('#loginbutton,#feedbutton').removeAttr('disabled');\r\n\t\tFB.getLoginStatus(updateStatusCallback);\r\n\t});\r\n});\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Problem with this approach is that you are changing global jQuery ajax cache settings.<\/p>\n<h2>The better way<\/h2>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script src=&quot;\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.0.0\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n$(document).ready(function()\r\n{\r\n\t$.ajax(\r\n\t\t\t{\r\n\t\t\t\turl: '\/\/connect.facebook.net\/en_US\/sdk.js',\r\n\t\t\t\tdataType: 'script',\r\n\t\t\t\tcache: true,\r\n\t\t\t\tsuccess:function(script, textStatus, jqXHR)\r\n\t\t\t\t{\r\n\t\t\t\t\tFB.init(\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tappId      : '{your-app-id}',\r\n\t\t\t\t\t\t\txfbml      : true,\r\n\t\t\t\t\t\t\tversion    : 'v2.8'\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t);\r\n\t\t\t\t\t\r\n\t\t\t\t\t$('#loginbutton,#feedbutton').removeAttr('disabled');\r\n\t\t\t\t\tFB.getLoginStatus(updateStatusCallback);\r\n\t\t\t\t}\r\n\t\t\t});\r\n});\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Now we did it. No race condition between jQuery, fb SDK for JS and loading of DOM. Also global jQuery Ajax settings are untouched. <\/p>\n<p>Cheers! :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8230; and avoid race condition! :) Although fb is pretty clear on how to do it (http:\/\/bit.ly\/2eGfFDv) \u2013 many developers are still doing it wrong. So this blog post is here to be reminder for me and others, also it will propose small upgrade to the fb recommended practice. The naive and wrong way to&#8230;<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,47,18,48],"tags":[43,42,44,46,45],"_links":{"self":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts\/2514"}],"collection":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/comments?post=2514"}],"version-history":[{"count":10,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts\/2514\/revisions"}],"predecessor-version":[{"id":2724,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts\/2514\/revisions\/2724"}],"wp:attachment":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/media?parent=2514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/categories?post=2514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/tags?post=2514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}