我该如何做到这一点?每次我尝试从Google地图加载外部JavaScript文件时,它都会使网页崩溃,并变为空白。动态加载外部JavaScript文件
我使用了$ JQuery.get();功能。
我正在使用JQuery将文件加载到头部。
我该如何做到这一点?每次我尝试从Google地图加载外部JavaScript文件时,它都会使网页崩溃,并变为空白。动态加载外部JavaScript文件
我使用了$ JQuery.get();功能。
我正在使用JQuery将文件加载到头部。
谷歌地图可能使用document.write
。这解释了空白页。这就是为什么document.write
是邪恶的。
尝试将google js文件下载到磁盘并搜索document.write
。如果它在那里,那么你的唯一选择是通常使用<script>
标签包含它。
我相信Google Maps API在使用上有一些限制,即使您没有立即使用它,在加载页面时也需要链接到它。您还需要最初在页面上拥有目标div(并在其正确的位置),即使它在加载时隐藏。
下面是我在我们的应用程序使用代码:
<html>
<head>
<title>geocoder test</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
function jsonp(src){
var s = document.createElement("script");
s.charset = "UTF-8";
s.src = encodeURI(src);
s.id = 'jsonp';
document.body.appendChild(s);
return s;
};
function showMap(json){
var mapCanvas = document.getElementById('map_canvas'),
jsonp = document.getElementById('jsonp');
if(json.Status.code !== 200){
mapCanvas.innerHTML = 'Address not found';
return false;
}
var ll = json.Placemark[0].Point.coordinates,
latlng = new google.maps.LatLng(ll[1], ll[0]),
myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(mapCanvas, myOptions),
marker = new google.maps.Marker({
position: latlng,
map: map
});
jsonp.parentNode.removeChild(jsonp);
}
function getMap(address){
jsonp([
'http://maps.google.com/maps/geo?',
'q=' + address,
'&output=json',
'&sensor=false',
//'&key= ... place here your key if not on localhost ...',
'&callback=showMap'
].join(''));
}
</script>
</head>
<body>
<input type="text" value="gran place, brussels"/><input type="button" onclick="getMap(this.previousSibling.value)" value="Get Map"/>
<div id="map_canvas" style="width:100%; height:450px"></div>
</body>
</html>
jQuery有所做的正是这样的功能:$.getScript
。例如:
$.getScript('/script.js', function()
{
alert('Script loaded!');
});
你能发表一些代码吗?你如何*加载*你的文件? – CMS 2010-01-04 21:23:05
你有代码摘录你如何做到这一点? – 2010-01-04 21:23:24
你试过jQuery的getscript吗? (http://docs.jquery.com/Ajax/jQuery.getScript) – Mottie 2010-01-04 22:22:16