好的,我已經找到了解決方案,我相信有人會說這是一個重複的話題,但我很困惑,爲什麼這是行不通的。bing map v7到v8遷移 - 腳本導入問題
這是我的代碼的一個基本的例子,現在
main.html中
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
<script>
var url =
//'http://www.bing.com/api/maps/mapcontrol?branch=experimental&callback=GetMap';
'https://www.bing.com/mapspreview/sdk/mapcontrol';
loadScript(url, test_function);
function test_function() {
console.log('inside test_function before map');
map = new Microsoft.Maps.Map('#myMap', {
credentials: 'My Bing Maps Key'});
console.log('inside test_function after map');
};
</script>
</html>
main.js
function loadScript(url, callback){
var script = document.createElement("script")
script.type = "text/javascript";
script.async = true;
script.defer = true;
if (script.readyState){ //IE
console.log('inside IE block');
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else { //Others
console.log('inside Other block');
script.onload = function(){
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
控制檯輸出
main.js:18 inside Other block
main.html:17 inside test_function before map
mapcontrol:11 Uncaught TypeError: Cannot read property 'prototype' of null
at k (mapcontrol:11)
at n.h [as create] (mapcontrol:11)
at e (mapcontrol:11)
at t.l [as instance] (mapcontrol:11)
at n.h [as create] (mapcontrol:11)
at e (mapcontrol:11)
at t.l [as instance] (mapcontrol:11)
at new Microsoft.Maps.Map (mapcontrol:13)
at test_function (main.html:18)
at HTMLScriptElement.script.onload (main.js:20)
我可以得到一個基本的示例工作,但是當我嘗試執行這種創建腳本元素並追加它的方法時,我什麼也得不到。由於這是它在更大的應用程序中實現的方式,因此它來自v7,我不想重新構建它。
我嘗試過像jQuery getScript之類的東西,而不是什麼。
請讓我知道你的想法!謝謝。