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