javascript - "FB is no defined" inside window.fbAsyncInit -
i'm implement editor can dynamic add together facebook button, write code:
utils.insertfacebooksdkjs = function (win) { var document = win.document; win.fbasyncinit = function () { fb.init({ appid: 'app id', // app id version:'v2.0', cookie: true, // enable cookies allow server access session xfbml: true // parse xfbml }); // additional initialization code here }; (function (d, s, id) { var js, fjs = d.getelementsbytagname(s)[0]; if (d.getelementbyid(id)) { return; } js = d.createelement(s); js.id = id; //js.async = true; js.src = "//connect.facebook.net/zh_cn/sdk.js"; fjs.parentnode.insertbefore(js, fjs); }(document, 'script', 'facebook-jssdk')); }; $('#add').click(function(){ //add <div class="fb-like"></div> utils.insertfacebooksdkjs(window); });
when user click add together button, function called. throw error: 'fb not defined'.
update: @ utils.insertfacebooksdkjs function, found if pass top window object parameter 'win', fb object fine. pass iframe window object parameter 'win', fb object not defined.
update: problem have solved. because facebook button in iframe. fb object field in iframe window. prepare fb win.fb @ utils.insertfacebooksdkjs
try version:
utils.insertfacebooksdkjs = function () { window.fbasyncinit = function () { fb.init({ appid: 'app id', // app id version:'v2.0', cookie: true, // enable cookies allow server access session xfbml: true // parse xfbml }); // additional initialization code here }; (function (d, s, id) { var js, fjs = d.getelementsbytagname(s)[0]; if (d.getelementbyid(id)) { return; } js = d.createelement(s); js.id = id; //js.async = true; js.src = "//connect.facebook.net/en_us/sdk.js"; fjs.parentnode.insertbefore(js, fjs); }(document, 'script', 'facebook-jssdk')); }; document.getelementbyid('add').addeventlistener('click', function() { utils.insertfacebooksdkjs(); });
removed unneccessary variables , changed locale (for testing). avoided jquery because should not utilize basic stuff click listeners imho.
i doing pretty much same in projects, if not work expect error @ different spot , test link may necessary.
javascript facebook
No comments:
Post a Comment