編集
あなたのコードは完全ではありません。コードを更新しました。これは私のドメインで機能するため、あなたのドメインでも機能する必要があります。この 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>
(アプリ ID を非表示にする必要はありません。それは js を介して表示され、指定されたドメインの外では、あなた以外の誰も使用できません。)