简体   繁体   中英

LinkedIn javascript API: Pass dynamic parameter

I want to pass API key dynamically to LinkedIn Library call,

I am getting that API key from a database field and saving it in global javascript variable 'myJavascriptVariable' but when I pass it to API on load . It is not accepting it and throwing exception.

What to do?

 <script type="text/javascript" src="//platform.linkedin.com/in.js?async=false">
        api_key:   myJavascriptVariable
        credentials_cookie: true
        authorize: true
</script>

EDIT: Error "You must specify a valid JavaScript API Domain as part of this key's configuration."

The content of that script element is not JavaScript. It is a configuration file used by LinkedIn's JavaScript. It cannot contain variables.

You might be able to use a JavaScript variable if you edit the configuration data (eg with innerHTML , but your timing would have to catch it before LinkedIn's JS tried to read it (which would almost certainly be before the load event fired).

You would be better off generating this server side .

I would dynamically create the script tag after you get the variable.

var LIScript = document.createElement('script');
LIScript.type = 'text/javascript';
LIScript.src = '//platform.linkedin.com/in.js?async=false';
LIScript.text = "api_key:  "+myJavascriptVariable+"
        credentials_cookie: true
        authorize: true";

document.getElementsByTagName('head')[0].appendChild(LIScript);

Although I am not sure this will solve your problem - you can use the defer and async attributes to control when during the load process the script is executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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