编辑
您的代码不完整,我更新了代码,这适用于我的域,因此,它必须也适用于您的 将此 js 代码放在 a.js 文件中,按下按钮时将调用它:
window.fbAsyncInit = function() {
FB.init({
appId : 'your_app_id', // App ID
channelUrl : 'your_app_domain', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Here we subscribe to the auth.authResponseChange JavaScript event
FB.Event.subscribe('auth.authResponseChange', function(response) {
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
// logged into the app
alert('logged');
} else if (response.status === 'not_authorized') {
// In this case, the person is logged into Facebook, but not into the app
alert('non logged');
FB.login();
} else {
// not logged into Facebook
alert('non logged everywhere');
FB.login();
}
});
};
// Load the SDK asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
然后把这段代码放到你想调用它的页面上,记住 fb-root div 和 fb 需要的所有其他东西:
<div id="fb-root"></div>
<div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1">
</div>
<button onClick="loadit()">ZzZzz</button>
<script type="text/javascript">
function loadit(){
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'a.js';
head.appendChild(script);
}
结束编辑
您可以通过以下方式从 javascript 加载脚本(例如,我们将文件称为 a.js): 您将 js 代码放入文件 a.js 检查它是否有效或需要一些改进
<button onClick="loadit()">ZzZzz</button>
<script type="text/javascript">
function loadit(){
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'a.js';
head.appendChild(script);
}
</script>
(你不需要隐藏app id,通过js可见,除了你,没有其他人可以在指定的域外使用它。)