2

Here is my code:

<!DOCTYPE html>
<html xmlns:fb="https://www.facebook.com/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.net</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.net/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.

12
  • Did you try just using "connect.facebook.net/en_US/all.js" instead of "//connect.facebook.net/en_US/all.js". Commented Apr 30, 2012 at 19:13
  • Doesn't work if I remove the "//" :-( Commented Apr 30, 2012 at 19:14
  • Drop the top script tag, you can't combine asynchronous and synchronous loading :) 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. Commented Apr 30, 2012 at 19:19
  • @Jack: Is this what you meant? I have edited the latest code. Commented Apr 30, 2012 at 19:23

3 Answers 3

2

Parse errors in your JavaScript should be the first things to address. Your code is missing a comma 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

Sign up to request clarification or add additional context in comments.

2 Comments

So dumb of me. I never started Tomcat server and went on double-clicking the file to check! :( Silly me - thanks to everyone!
I did mention that in my answer as a possible reason :p
1

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

8 Comments

Trying he changes you suggested now .
Where do I remove the script tag John? I edited the latest code I have above.
@AbhishekShivkumar It looks like you removed it properly :) (saw your post edit)
But I don't get the alert box yet... wondering what the problem is here!
@AbhishekShivkumar You should accept the correct answer or upvote helpful comments :p
|
1

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.