admin管理员组

文章数量:1434929

Here is my code:

<!DOCTYPE html>
<html xmlns:fb="">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>New JavaScript SDK & OAuth 2.0 based FBConnect Tutorial |     
Thinkdiff</title>
</head>

<body>

<div id="fb-root"></div>

<script>

  window.fbAsyncInit = function() {
  FB.init({
  appId      : '154062364721089', // App ID
  channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true,  // parse XFBML
  oauth: true
  });

  // Additional initialization code here

   alert('Api loaded');

   FB.login(function(response)
   {
    console.log(response);
   });

   };

  // 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/en_US/all.js";
 ref.parentNode.insertBefore(js, ref);
 }(document));

</script>

test

</body>
</html>

But when I open this file in the browser the Facebook api does not load. I just see "test" printed on the screen. I believe the browser should be contacting facebook to load the api - this I think will be known as a connecting message at the bottom of the browser, which is not happening.

Here is my code:

<!DOCTYPE html>
<html xmlns:fb="https://www.facebook./2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>New JavaScript SDK & OAuth 2.0 based FBConnect Tutorial |     
Thinkdiff</title>
</head>

<body>

<div id="fb-root"></div>

<script>

  window.fbAsyncInit = function() {
  FB.init({
  appId      : '154062364721089', // App ID
  channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true,  // parse XFBML
  oauth: true
  });

  // Additional initialization code here

   alert('Api loaded');

   FB.login(function(response)
   {
    console.log(response);
   });

   };

  // 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/en_US/all.js";
 ref.parentNode.insertBefore(js, ref);
 }(document));

</script>

test

</body>
</html>

But when I open this file in the browser the Facebook api does not load. I just see "test" printed on the screen. I believe the browser should be contacting facebook to load the api - this I think will be known as a connecting message at the bottom of the browser, which is not happening.

Share Improve this question edited Sep 29, 2013 at 22:10 user212218 asked Apr 30, 2012 at 19:11 London guyLondon guy 28.1k47 gold badges127 silver badges182 bronze badges 12
  • Did you try just using "connect.facebook/en_US/all.js" instead of "//connect.facebook/en_US/all.js". – Larry Battle Commented Apr 30, 2012 at 19:13
  • Doesn't work if I remove the "//" :-( – London guy Commented Apr 30, 2012 at 19:14
  • Drop the top script tag, you can't bine asynchronous and synchronous loading :) – Ja͢ck Commented Apr 30, 2012 at 19:17
  • I think you need to close the script tag when you use the src attribute. Then open a new script tag when you want to write javascript. – Larry Battle Commented Apr 30, 2012 at 19:19
  • @Jack: Is this what you meant? I have edited the latest code. – London guy Commented Apr 30, 2012 at 19:23
 |  Show 7 more ments

3 Answers 3

Reset to default 2

Parse errors in your JavaScript should be the first things to address. Your code is missing a ma between the last two dictionary elements within the FB.init() call (xfbml and oauth)

Edit The code as you have it works fine for me in IE and Chrome, except when you run it from your local machine (i.e. double click an HTML file on your disc) due to the '//' in the URL when loading the API

It sounds like you're expecting Facebook to automatically prompt the user to connect/allow/login to the App. To do this, you need to add the following after fb api initialization (code goes after // Additional initialization code here)...

// Additional initialization code here

alert('Api loaded');

FB.login(function(response)
{
    console.log(response);
});

Edit: Also, try removing the <script> reference and use the async code from the facebook documentation - https://developers.facebook./docs/reference/javascript/

  (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/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));

Not sure if this is totally relevant, found this thread as I've been getting 'js is not defined' after the line: js = d.createElement('script'); js.id = id; js.async = true;

Turned out the solution was the line: if (d.getElementById(id)) {return;} needing a trailing ; after, and not within the line parenthesis:

if (d.getElementById(id)) {return;};

and

if (d.getElementById(id)) {return};

Worked, whereas:

if (d.getElementById(id)) {return;}

Throws the error..

本文标签: Facebook Javascript api not loadingStack Overflow