如何使用脚本加载器加载 LinkedIn Javascript API 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9071249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I load the LinkedIn Javascript API library with a script loader?
提问by wilsonpage
The LinkedIn Api suggests you load their javascript library like this:
LinkedIn Api 建议您像这样加载他们的 javascript 库:
<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!
我想知道如何使用脚本加载器(例如 RequireJS 或 LABJS)加载它。该库似乎从脚本标签中提取了 api 密钥。在我看来,这似乎是一种非常奇怪的做法!
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.
我更喜欢使用脚本加载器加载库,但似乎无法找到如何在不使用建议的方法的情况下插入 api_key。
The official instructions are here
Anyone have any ideas?
有人有想法么?
回答by Adam Trachtenberg
From: https://developer.linkedin.com/documents/general-methods
来自: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:
如果您的页面使用 JQuery,则以下代码将起作用:
$(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>
回答by Muhammad Tahir
Check this out
看一下这个
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");
}
});
});
}
回答by Ujjwal Singh
As noted by @AdamTrachtenberg you need to use the async version of the API: http://platform.linkedin.com/in.js?async=true
正如@AdamTrachtenberg 所指出的,您需要使用 API 的异步版本:http://platform.linkedin.com/in.js?async=true
next you will have to call the In.init()
upon load of the API JS.
You should do so in the callback function of you script loader.
接下来,您将不得不In.init()
在 API JS 加载时调用。
您应该在脚本加载器的回调函数中执行此操作。
You may provide your API Key as a param to In.init()
您可以将您的 API 密钥作为参数提供给 In.init()
Note: that you do notneed to pass a callback function onLoad
to In.init()
a post i wrote about the same
注意:你不是需要一个回调函数传递onLoad
到In.init()
后我写的关于同一