我的 HTML head 元素中有以下脚本。
<script src="" id="mapScriptID" type="text/javascript"></script>
<script type="text/javascript">
var resloc = "http://maps.google.com/maps/api/js?sensor=true&key="+key;
var mapScript = document.getElementById('mapScriptID');
mapScript.src = resloc;
</script>
原因是我希望 google map api 的键值仅定义一次,因为我在应用程序的多个位置使用它。这样将来当键值发生变化时我只需在一处进行更改即可。但现在由于某种原因,应用程序不再看到谷歌地图 api。当我调用 google.maps.LatLng(....); 时,它给我“google 未定义”消息;
以前是
<script src="http://maps.google.com/maps/api/js?sensor=true&key=xxxxxxxxxxxxxxxxxxxxxxxx" type="text/javascript"></script>
它工作得很好。
最佳答案
尝试多种解决方案后。最终我使用了 JQuery 的 $.getScript() 方法,它在我的场景中运行得很好。
关于javascript - 在 HTML 的 <script> src 属性中传递 Javascript 全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56644793/