How can I load the LinkedIn Javascript API library with a script loader?

wilsonpage picture wilsonpage · Jan 30, 2012 · Viewed 11.9k times · Source

The LinkedIn Api suggests you load their javascript library like this:

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: your_api_key_goes_here
</script>

I want to know how I can load this using a script loader (eg. RequireJS or LABJS). It seems the library extracts the api key from within the script tags. This seems like a pretty weird way of doing it in my opinion!

I would prefer to load the library using a script loader, but can't seem to find out how to insert the api_key without using the suggested method.

The official instructions are here

Anyone have any ideas?

Answer

Adam Trachtenberg picture Adam Trachtenberg · Feb 1, 2012

From: https://developer.linkedin.com/documents/general-methods

Async Loading

To avoid encountering race conditions in your page, you can load the framework asynchronously.

If your page uses JQuery, the following code will work:

$(document).ready(function() {
    $.getScript("http://platform.linkedin.com/in.js?async=true", function success() {
        IN.init({
            onLoad: "myOnloadFunction"
        });
    });
});

Otherwise, you need something like this:

<script type="text/javascript" src="http://platform.linkedin.com/in.js?async=true"></script>
<script type="text/javascript">
    IN.init({
        onLoad: "myOnloadFunction"
        // any other parameters you'd normally put beneath the script element would be here
    });
</script>