我可以使用JavaScript动态更改/设置Google Maps API密钥吗?
我正在开发一个小型Web应用程序时遇到问题:我的HTML源代码将被集成到另一个网站上的HTML源代码中。我在我的代码中使用Google Map,所以我必须通过一个API密钥来加载当前域上的Google Maps脚本。
I'm faced with a problem with a small web application I'm developping: My HTML-source will be integrated into the HTML source on another site. I'm using a Google Map in my code, so I have to pass a API-Key for loading the Google Maps-script on the current domain.
问题:我的代码将被集成到两个不同的域中,需要两个不同的API密钥。我有这两个键,可以通过JavaScript识别有效的键(在document.location.host的帮助下),但我怎样才能设法用正确的键动态加载脚本?
The problem: My code will be integrated on two different domains, requiring two different API-Keys. I have those two keys and can identify the valid one by JavaScript (With the help of document.location.host), but how can I manage to dynamically load the script with the correct key?
供参考:该键作为脚本加载url中的参数传递:
For reference: The key is passed as parameter in the script loading url:
<script src="http://maps.google.com/maps?file=api&v=2&key=abcdefg" type="text/javascript">
</script>
使用
Use
var script = document.createElement("script");
script.setAttribute("src",whatever);
document.getElementsByTagName("head")[0].appendChild(script);
替换您想要使用的脚本源代码
Replace whatever with the script source you want to use