繁体   English   中英

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

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

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。

官方说明在这里

有人有主意吗?

来自: 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>

看一下这个

 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");
                    }
                });
            });
        }

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

接下来,您必须在加载API JS时调用In.init()
您应该在脚本加载器的回调函数中执行此操作。

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

注意:您并不需要一个回调函数传递onLoadIn.init()
我写的一篇文章也是如此

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM