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

标签 javascript html

我的 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/

相关文章:

java - 如何在桌面应用程序中使用电子邮件的 HTML 模板?

javascript - jQuery nextUntil 属性小于或等于值

html - 如何使用 CSS 为所有浏览器垂直居中 div 元素?

javascript - 使用导出模块将多个参数传递给 NodeJS 回调

javascript - 我如何居中新的谷歌地图标记

php - 单击函数,Window.location

html - 除非 li 悬停,否则如何让我的下拉菜单不打开?

html - 在页脚小部件中居中图像

javascript - jQuery Mobile,使用查询字符串进行导航

javascript - 是否可以使用 JavasScript 确定 GeoJSON 点是否在 GeoJSON 多边形内?