0

I am creating a bookmarklet which inserts facebook's all.js in the page in the head however it is not working without errors either, here it is:

javascript: (function() {
    var msg = '';
    if (typeof window.FB == 'object') {
        msg = 'Already Facebookified';
    } else {
        var js = document.createElement('script');
        js.id = 'facebook-jssdk';
        js.async = true;
        js.src = "http://connect.facebook.net/en_US/all.js";
        document.getElementsByTagName('head')[0].appendChild(js);
        msg = (typeof window.FB == 'object' ? 'Facebookified': 'Unable to Facebookify');
    }

    alert(msg);

})();

Can anyone suggest what could be wrong with it ?

3
  • scripts are loaded asynchronously, you'll need to listen for the script to be finished executing
    – zzzzBov
    Commented Jan 30, 2012 at 20:02
  • @zzzzBov: I don't get that, can you elaborate what change should i make in the code ?
    – Sarfraz
    Commented Jan 30, 2012 at 20:10
  • i think my answer to this question is probably relevant.
    – zzzzBov
    Commented Jan 30, 2012 at 20:31

3 Answers 3

2

There are some common mistakes made when trying to load a script asynchronously. This code should work:

function loadScript(src, callback) {
  var s,
      r;
  s = document.createElement('script');
  s.src = src;
  s.onload = s.onreadystatechange = function () {
/**
 * LOAD/READYSTATE LOGIC
 * execute if the script hasn't been ready yet and:
 * - the ready state isn't set
 * - the ready state is complete
 *   - note: readyState == 'loaded' executes before the script gets called so
 *     we skip this event because it wouldn't have loaded the init event yet.
 */
    if (!r && (!this.readyState || this.readyState === 'complete')) {
      //set the ready flag to true to keep the event from initializing again
      r = true;
      callback();
    }
  };
  document.body.appendChild(s);
}

You could use it as:

loadScript('http://connect.facebook.net/en_US/all.js', function () {console.log(window.FB)}
0
2

You probably want to use window.fbAsyncInit to run code once Facebook JS-SDK is loaded and add fb-root node (which is needed for some functionality):

javascript: (function() {
  var msg = '';
  if (typeof window.FB == 'object') {
    alert('Already Facebookified');
  } else {
    window.fbAsyncInit = function(){
      alert('JS-SDK loaded');
    };
    var d = document,
        js = d.createElement('script'),
        root_node = d.createElement('div');
    js.id = 'facebook-jssdk';
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    root_node.id = 'fb-root';
    d.getElementsByTagName('body')[0].appendChild(root_node);
    d.getElementsByTagName('head')[0].appendChild(js);
  }
})();
2
  • Thanks but it gives back this error on console: Uncaught SyntaxError: Unexpected token var when I run it from bookmark.
    – Sarfraz
    Commented Jan 30, 2012 at 20:31
  • I've fixed the snipped (it was due to fact that all lines are combined, and semicolon was needed after curly brace on line 8) Commented Jan 30, 2012 at 20:35
0

As zzzzBov has pointed out, you will need to "listen" for the script to be done loading. To that end there are at least two events for script elements that you can hook on to (onreadystatechange and onload). You can set them as follows:

javascript: (function() {
    var isFacebookified = function () {
        return typeof window.FB === 'object';
    };
    var scriptIsDoneLoading = function () {
        var msg = (isFacebookified ? 'Facebookified': 'Unable to Facebookify');
        alert(msg);
    };
    if (isFacebookified()) {
        alert('Already Facebookified');
    } else {
        var js = document.createElement('script');
        js.id = 'facebook-jssdk';
        js.async = true;
        js.src = "http://connect.facebook.net/en_US/all.js";
        js.onreadystatechange = scriptIsDoneLoading;
        js.onload = scriptIsDoneLoading;
        document.getElementsByTagName('head')[0].appendChild(js);
    }
})();

Once the script has finished loading, you should get your alert.

Hope this helps,

Pete

2
  • It does not help either. When I put it in bookmark and run, there are no errors and I get the message Already Facebookified however with firebug console.log(typeof window.FB) returns undefined
    – Sarfraz
    Commented Jan 30, 2012 at 20:40
  • Whoops! if (isFacebookified) { should be if (isFacebookified()) { otherwise we're checking for existence of the function not invoking it. After adding the extra parens, it works for me. Edited response to reflect correct code.
    – pete
    Commented Jan 30, 2012 at 22:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.