Monday, February 13, 2012

Running JQuery or Javascript on Facebook


I am trying to execute a simple bit of jQuery or JavaScript once logged in to my Facebook account. The end goal at this moment is to click on my custom feed once the element is visible, rather than having it default to the entire Facebook friend feed.



On trying to code this using my Developer's Console in Chrome no JavaScript/jQuery seems to work at all. Even just a simple $('div'); returns null . Is it possible to run code on Facebook's page? What are they doing differently?



I guess the end goal would be to click a div if the .val('give a waffle'); but just understanding why it has not worked until now would also be interesting.



Any hints, tips or suggestions are more than welcome.

2 comments:

  1. Facebook doesn't use jQuery, if you want to play with their page using jQuery commands you'll need to load it. Just copy the contents of http://code.jquery.com/jquery-1.7.1.min.js and paste it into your console first. Then proceed as usual.

    Additionally you could let Google host your jQuery :

    <script type="text/javascript"
    src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    // This is more like it!
    });
    </script>


    Many sites do this so the chances of your user already having the library cached is relatively high.

    ReplyDelete
  2. if("Developer's Console" == "https://developers.facebook.com/tools/console/") {
    answer = "no";
    } else {
    throw new Exception("unknown error");
    }

    ReplyDelete