除非调用onclick,否则脚本不会运行

时间:2016-01-06 21:58:28

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

以下代码在设置为名为onclick

的功能时有效



<script type="text/javascript">
  function Nome_evento() {
  FB.api("/1718063768425398",
    function (response) {
      if (response && !response.error) {
        /* handle the result */
		document.getElementById("evento").innerHTML = response.name;
      }
    });
		}
</script>
&#13;
&#13;
&#13;

但是什么时候

&#13;
&#13;
<script type="text/javascript">
    FB.api("/1718063768425398",
    function (response) {
      if (response && !response.error) {
        /* handle the result */
		document.getElementById("evento").innerHTML = response.name;
      }
    });
</script>
&#13;
&#13;
&#13; 它只是不执行脚本内部的任何内容,任何想法为什么?我真的坚持这个。

以下是进一步背景的完整html

&#13;
&#13;
<html>
	<?php
	  session_start();
	  $loader = require __DIR__ . '/vendor/autoload.php';
	?>
	<head>
		<title>Ember</title>
	</head>
	<body>
		<h1>Using Facebook API tests space</h1>
		<div id="fb-root"></div>
		<script src="js/face_init_js.js"></script>
		<script>
			
			FB.Event.subscribe('auth.authResponseChange', function (response) {
				  if (response.status === 'connected') {
					alert("Successfully connected to Facebook!");
				  }
				  else if (response.status === 'not_authorized') {
					alert("Login failed!");
				  } else {
					alert("Unknown error!");
				  }
				});	
		</script>
		<input type="button" value="Login" onclick="Login()" />
		<input type="button" value="Evento" onclick="Nome_evento()" />
		<div id="displayName" style="padding-top: 20px">You haven't logged in yet.</div>
		<div id="userName" style="padding-top: 20px">You haven't logged in yet.</div>
		<div id="userID" style="padding-top: 20px">You haven't logged in yet.</div>	
		<div id="userEmail" style="padding-top: 20px">You haven't logged in yet.</div>
		<script>
			function Login() {
			  alert("comecou login!");
			  FB.login(function (response) {
				if (response.authResponse) {
				  // some code here
					FB.api('/me', function (response) {
        document.getElementById("displayName").innerHTML = response.name;
        document.getElementById("userName").innerHTML = response.username;
        document.getElementById("userID").innerHTML = response.id;
        document.getElementById("userEmail").innerHTML = response.email;
      });
				} else {
				  alert("Login attempt failed!");
				}
			  }, { scope: 'email,user_photos,publish_actions' });

			}
		</script>
		<div id="evento" style="padding-top: 20px">Evento</div>
		<script type="text/javascript">
		 FB.api("/1718063768425398",
    function (response) {
      if (response && !response.error) {
        /* handle the result */
		document.getElementById("evento").innerHTML = response.name;
      }
    });
		</script>
	</body>
</html>
&#13;
&#13;
&#13;

- 编辑这是face_init_js.js,FB函数(如登录)正在运行

&#13;
&#13;
window.fbAsyncInit = function () {
			  FB.init({
				appId: '*****************',
				status: true,
				cookie: true,
				xfbml: true,
				version    : 'v2.5'
			  });
			};

(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/sdk.js";
				 fjs.parentNode.insertBefore(js, fjs);
			   }(document, 'script', 'facebook-jssdk'));
&#13;
&#13;
&#13;

2编辑 -

&#13;
&#13;
<script type="text/javascript">

		window.fbAsyncInit = function () {
			  FB.init({
				appId: '505610319610868',
				status: true,
				cookie: true,
				xfbml: true,
				version    : 'v2.5'
			  });
			FB.api("/1718063768425398",
    function (response) {
      if (response && !response.error) {
        /* handle the result */
		document.getElementById("evento").innerHTML = response.name;
      }
    });
			};
		</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您只能在加载和初始化后使用JavaScript SDK:

window.fbAsyncInit = function() {
    //SDK loaded, initialize it
    FB.init({
        appId      : 'your-app-id',
        xfbml      : true,
        version    : 'v2.5'
    });

    //now you can use FB
};

链接:

如果这对您没有帮助,并且在您尝试使用FB时浏览器控制台中没有错误,请在回调函数的开头放置一个console.log。换句话说:调试代码。