jquery - Can't call function inside of jQuery plugin for a facebook login event
问题描述
I am trying to create a jQuery plugin which will ask for appID and create the facebook login option for the user.
My code is :
; (function($){
$.fn.fbLogin = function(options){
var defaults = {
appId: ''
};
var opt = $.extend(defaults, options);
var fbRoot = "<div id='fb-root'></div>";
var fbSdkLoad = "<script async defer crossorigin='anonymous' src='https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.3&appId=" + opt.appId + "&autoLogAppEvents=1'></script>";
var fbLoginButton = `<div
class="fb-login-button"
data-width=""
data-size="large"
data-button-type="login_with"
data-auto-logout-link="false"
data-use-continue-as="true"
data-scope="email"
data-onlogin="loginFB();"
/>`;
$("body").append(fbRoot);
$("body").append(fbSdkLoad);
$("body").append(fbLoginButton);
function loginFB(){
console.log("yes");
}
}
})(jQuery);
when I load this plugin in my index.html file then Login with Facebook button is loaded. When I click it, a popup appears and then I got the following error in my dev console.
Uncaught ReferenceError: loginFB is not defined
When I add the loginFB function in index.html in a script, it works, which makes sense as login button is there in body scope, and can call the function.
But I want to have a function which will do some work after login which needs to be stayed inside the plugin function.
Can anyone suggest me any better approach for this ?
解决方案
推荐阅读
- wordpress - Wordpress website hacked and .htaccess file is creating automatically with strange code
- javascript - Changing Image position when scrolling page
- html - 在悬停时使背景变暗
- c# - 指定的可执行文件不是此 OS 平台的有效应用程序。
- ansible - 如何使用 ansible 下载和提取 apache tomcat 并重命名 unzaip 文件
- javascript - Javascript变量在onload函数内没有改变
- python - pycharm 中的警告,带有 sort(key = len) 函数
- postgresql - 如何解决空 docker-entrypoint-initdb.d 的问题?(PostgreSQL + Docker)
- python - Python:通过将对象分配给另一个对象来修改它
- javascript - 让 ng-view 只显示它的内容,而不是它本身