【问题标题】:How can I load the LinkedIn Javascript API library with a script loader?如何使用脚本加载器加载 LinkedIn Javascript API 库?
【发布时间】:2015-11-13 14:46:53
【问题描述】:

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。

The official instructions are here

有人有什么想法吗?

【问题讨论】:

    标签: javascript linkedin


    【解决方案1】:

    发件人: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>
    

    【讨论】:

    • 在 IN.init() 部分中(其中显示“您通常放在脚本元素下方的任何其他参数都会放在此处”),因此您将添加 api_key: your_api_key_goes_here 作为参数那个函数调用
    • 初始化api_key后,还需要运行IN.parse()吗?如:$.getScript(me.linkedInScript, function() { IN.init({ api_key: me.linkedInApiKey }); IN.parse(); })
    • 如前所述,/general-methods 的链接现在已断开。
    • 添加?async=true 为我解决了这个问题。然后拨打IN.UI.Authorize().place();
    【解决方案2】:

    看看这个

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

    【讨论】:

      【解决方案3】:

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

      接下来,您必须在加载 API JS 时调用 In.init()
      您应该在脚本加载器的回调函数中这样做。

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

      注意:您确实不需要需要将回调函数onLoad 传递给In.init()
      a post i wrote about the same

      【讨论】:

        猜你喜欢
        • 2019-03-12
        • 1970-01-01
        • 1970-01-01
        • 2016-05-22
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多