0
如何添加一些JavaScript只加載谷歌地圖API:只有當我的應用程序是navigator.onLine時,如何加載google maps API?
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
當我的應用程序在線(navigator.onLine == true
)?
如何添加一些JavaScript只加載谷歌地圖API:只有當我的應用程序是navigator.onLine時,如何加載google maps API?
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
當我的應用程序在線(navigator.onLine == true
)?
使用動態腳本插入:
if(navigator.onLine) {
var script = document.createElement('script');
//You must use the callback parameter when loading the script asynchronously
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=initialize';
var scripts = document.getElementsByTagName('script');
scripts[0].parentNode.insertBefore(script, scripts[0]);
}
function initialize() {
//initialize map here
}
見Asynchronous Loading topic在Google Maps API v3 Documentation。
當你在網上反正會腳本無法加載_only_? – davidkonrad
是的,但然後我會得到一個錯誤 – gurehbgui