2015-09-06 50 views
2

我有main.html中網頁,我打電話子頁面在main.html中是這樣的:谷歌地圖頁面沒有顯示阿賈克斯加載的內容

main.html中

<html> 
<head> 
<title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
</head> 
<body> 
<script type="text/javascript"> 
function getmyurl(url){ 
$('#mypageload').load(url); 
} 
</script> 
<a href="javascript:void(0);" onclick="getmyurl('googlemap.htm');">google map</a> 
<div id="mypageload">html page content will come to here like php include</div> 
</body> 
</html> 

這是工作好對於普通頁面。但谷歌地圖(googlemap.htm)沒有顯示地圖。我猜這裏的問題:

google.maps.event.addDomListener(window, 'load', initialize); 

我該如何解決修復問題?

googlemap.htm

<html> 
<head> 
<title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> 
</head> 
<body> 
<script> 
function initialize() { 
.... 
} 
</script> 
<div id="map-canvas"></div> 
</body> 
</html> 

注:我的英語不是很好,我爲miskates遺憾。

回答

3

我用你的代碼獲取(最好我能複製它)的錯誤是:

未能在「文檔」執行「寫」:這是不可能寫入一個文件從異步已加載的外部腳本,除非它明確打開。

您可以修復,通過asynchronously loading the API

function loadScript(src,callback){ 

    var script = document.createElement("script"); 
    script.type = "text/javascript"; 
    if(callback)script.onload=callback; 
    document.getElementsByTagName("head")[0].appendChild(script); 
    script.src = src; 
} 
loadScript('http://maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=initialize'); 

proof of concept fiddle

+0

謝謝您的回答和有用的例子。我現在解決了我的問題。 – webmaster

+0

您的建議對我無效,但在任何情況下,您的示例代碼中都有拼寫錯誤。回調應該是第二個參數,而不是查詢字符串的一部分。 – Vincent

0

由於谷歌地圖支持異步負荷,你可以通過應用以下更改實現它:

替換googlemap.htm檔案行:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> 

<script src="https://maps.googleapis.com/maps/api/js?callback=initialize&sensor=false&libraries=places" async defer></script> 

由於callback參數指定一旦谷歌地圖庫加載時調用的函數名稱,註釋(或刪除)在googlemap.htm文件:

//google.maps.event.addDomListener(window, 'load', initialize); 
+1

感謝您提供更多有用的信息。 – webmaster