Facebook instructs us to put the following code on our page:
<div id="fb-root"></div> <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>
Instead, I wrap it in a function:
function ReloadSocialSharing() { //facebook (function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=myappid"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); }
and name it inside: $(function () { }
Now my similar buttons and my comments through the Facebook panel appear and work fine. Then I make an AJAX call and insert the new html elements into my page. They already have matching Facebook labels similar to my original fields. I make my ajax call like this:
$('#search').ajaxSubmit(GetSearchAjaxFormOptions(!isDefault, element)); function GetSearchAjaxFormOptions(displayResetPreferencesButton, giveMeFocus) { return { target: '#search-results', data: GetSearchData(), success: function () { RunAfterSearchResultsAreReturned(giveMeFocus) }, error: function (error) { alert("Oops. There was an error.") } }; } RunAfterSearchResultsAreReturned() { ........ ReloadSocialSharing(); }
All of this works well, but the new elements of social sharing that were added through the Ajax call are not connected and do not work. What am I missing?
Thanks!
Note. I saw notes about using FB.XFBML.parse (); However, I am trying to avoid XFBML.