问题 如何使用脚本加载器加载LinkedIn Javascript API库?


LinkedIn Api建议您加载他们的javascript库,如下所示:

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

我想知道如何使用脚本加载器(例如RequireJS或LABJS)加载它。似乎库从脚本标记中提取了api密钥。在我看来,这似乎是一种非常奇怪的方式!

我更喜欢使用脚本加载器加载库,但似乎无法在不使用建议的方法的情况下找到如何插入api_key。

官方说明在这里

有人有主意吗?


1357
2018-01-30 21:45


起源



答案:


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

异步加载

为避免在页面中遇到竞争条件,您可以异步加载框架。

如果您的页面使用JQuery,则以下代码将起作用:

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

否则,你需要这样的东西:

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

15
2018-02-01 20:49



那我的API密钥在哪里? - wilsonpage
在IN.init()部分(其中说“你通常放在脚本元素下面的任何其他参数都会在这里”),所以你要添加api_key:your_api_key_goes_here作为该函数调用的参数 - Kirsten Jones
初始化api_key后,你还需要运行IN.parse()吗?如: $.getScript(me.linkedInScript, function() { IN.init({ api_key: me.linkedInApiKey }); IN.parse(); }) - Elise Chant
developer.linkedin.com/documents/general-methods 被打破 - ankitr
如前所述,链接到 /general-methods 现在坏了。 - theblang


答案:


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

异步加载

为避免在页面中遇到竞争条件,您可以异步加载框架。

如果您的页面使用JQuery,则以下代码将起作用:

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

否则,你需要这样的东西:

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

15
2018-02-01 20:49



那我的API密钥在哪里? - wilsonpage
在IN.init()部分(其中说“你通常放在脚本元素下面的任何其他参数都会在这里”),所以你要添加api_key:your_api_key_goes_here作为该函数调用的参数 - Kirsten Jones
初始化api_key后,你还需要运行IN.parse()吗?如: $.getScript(me.linkedInScript, function() { IN.init({ api_key: me.linkedInApiKey }); IN.parse(); }) - Elise Chant
developer.linkedin.com/documents/general-methods 被打破 - ankitr
如前所述,链接到 /general-methods 现在坏了。 - theblang


正如@AdamTrachtenberg所述,您需要使用API​​的异步版本: http://platform.linkedin.com/in.js?async=true

接下来你将不得不打电话给 In.init() 在加载API JS时。
您应该在脚本加载器的回调函数中执行此操作。

您可以提供API密钥作为参数 In.init()

注意:你这样做  需要传递一个回调函数 onLoad 至 In.init()
我写的一篇文章也是如此


0
2018-01-08 13:03





看一下这个

 if(typeof IN === 'undefined'){ //if it is already included don't include that
            $.getScript('//platform.linkedin.com/in.js', function(data, textStatus){
                 IN.init({
                    api_key: 'YOUR_API_KEY',
                    onLoad: function(){
                        alert("Fully Loaded");
                    }
                });
            });
        }

0
2017-11-13 06:46