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 ?

Dev555
  • 2,128
  • 4
  • 30
  • 40

3 Answers3

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);
  }
})();
Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
  • Thanks but it gives back this error on console: `Uncaught SyntaxError: Unexpected token var` when I run it from bookmark. – Dev555 Jan 30 '12 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) – Juicy Scripter Jan 30 '12 at 20:35
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)}
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
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

Community
  • 1
  • 1
pete
  • 24,141
  • 4
  • 37
  • 51
  • 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` – Dev555 Jan 30 '12 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 Jan 30 '12 at 22:05