{"id":1752,"date":"2022-08-30T15:19:09","date_gmt":"2022-08-30T15:19:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/30\/fb-is-undefined-even-though-i-just-used-it-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:09","modified_gmt":"2022-08-30T15:19:09","slug":"fb-is-undefined-even-though-i-just-used-it-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/fb-is-undefined-even-though-i-just-used-it-collection-of-common-programming-errors\/","title":{"rendered":"FB is undefined even though I just used it-Collection of common programming errors"},"content":{"rendered":"<p>I had this problem and solved it similarly to the solution that Amry provided so I&#8217;m posting it here in case someone else needs to use it.<\/p>\n<p>I made the initial assumption that the FB initialization call made in the fbAsyncInit method would initialize immediately, so I too coded my use of the FB object in a <code>$(document).ready()<\/code> method. That is not the case. fbAsyncInit takes quite some time after the page loads to finish initializing. Here is my solution, when using jQuery on my site:<\/p>\n<pre><code>\n\n  window.fbAsyncInit = function() {\n\n    FB.init({\n      appId      : 'your_app_id', \/\/ App ID\n      channelUrl : 'your channel',\n      status     : true, \/\/ check login status\n      cookie     : true, \/\/ enable cookies to allow the server to access the session\n      xfbml      : true  \/\/ parse XFBML\n    });\n    jQuery(document).trigger('FBSDKLoaded'); \/\/This is the most important line to solving the issue\n  };\n\n  \/\/ Load the SDK Asynchronously\n  (function(d){\n     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];\n     if (d.getElementById(id)) {return;}\n     js = d.createElement('script'); js.id = id; js.async = true;\n     js.src = \"\/\/connect.facebook.net\/en_US\/all.js#xfbml=1&amp;appId=269187653191150\";\n     ref.parentNode.insertBefore(js, ref);\n   }(document));\n\n\n<\/code><\/pre>\n<p>The last task is to simply switch your <code>$(document).ready()<\/code> code to a bind for your event. This goes where ever you are using the FB object.<\/p>\n<pre><code>(function($) {\n    $(document).bind('FBSDKLoaded', function() {\n        \/\/Code using the FB object goes here.\n    });\n\n})(jQuery);\n<\/code><\/pre>\n<p>One thing I should also mention: Firefox is a lot more tolerant of this than Webkit browsers like Chrome. I couldn&#8217;t figure out why NONE of my jQuery was working properly in Chrome&#8230; well, this was why.<\/p>\n<p>I hope this helps someone.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-30 21:27:41. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I had this problem and solved it similarly to the solution that Amry provided so I&#8217;m posting it here in case someone else needs to use it. I made the initial assumption that the FB initialization call made in the fbAsyncInit method would initialize immediately, so I too coded my use of the FB object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1752","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1752"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1752\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}