gpt4 book ai didi

javascript - 在 HTML 的 <script> src 属性中传递 Javascript 全局变量

转载 作者:行者123 更新时间:2023-12-02 23:28:00 24 4
gpt4 key购买 nike

我的 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 的 &lt;script&gt; src 属性中传递 Javascript 全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56644793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com