我正在创建一个使用 google map api 的 asp.net 页面。该页面在包含谷歌地图初始化内容的 head 标记中加载 GoogleMap.js。在后面的代码中,我使用 Page.ClientScript.RegisterClientScriptBlock() 注入了更多的 javascript 代码(我也尝试过 RegisterStartupScript())。此函数将额外注入的代码放置在 body 标记内的脚本块中(在 GoogleMap.js 脚本下方)。我正在使用 Chrome 开发人员窗口来调试我的 javascript,现在当我输入一些断点来检查执行顺序时,正文中的脚本块在头部脚本之前执行。所以它不像我认为的那样从 HTML 页面的顶部到底部按顺序进行。这是怎么回事???我该如何解决?为了组织起见,我
问题是body标签首先执行,并且由于没有为“map”变量分配谷歌地图任何对象,因此不会添加标记。
这是asp.net生成并返回到客户端浏览器的一些代码...
</title><link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css" /><link href="../css/master.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
function initialize() {
var chicago = new google.maps.LatLng(41.879535, -87.624333);
var mapOptions = {
zoom: 4,
center: chicago,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var marker = new google.maps.Marker({
position: chicago,
map: map,
title: "Hello World!"
});
$('#Debug').html(CountryDropDownList.children("option").filter(":selected").text().trim());
}
$(document).ready(function() {
initialize();
});
</script>
<script src="SearchTabs.js" type="text/javascript"></script>
<!--[if gte IE 9]>
<style type="text/css">
.gradient {
filter: none;
}
</style>
<![endif]-->
<style type="text/css">
.ctl00_MainMenu_0 { background-color:white;visibility:hidden;display:none;position:absolute;left:0px;top:0px; }
.ctl00_MainMenu_1 { text-decoration:none; }
.ctl00_MainMenu_2 { }
.ctl00_MainMenu_3 { font-size:12pt; }
.ctl00_MainMenu_4 { padding:0px 10px 0px 10px; }
</style></head>
<body>
<form name="aspnetForm" method="post" action="HertzMap.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="VIEWSTATE_CRAP" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<script type="text/javascript">
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(41.79068,-77.26577),
map: map,
title: "WHEEL LOADER/3YD/GEN BKT/DSL "
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(41.72962,-76.12181),
map: map,
title: "CRAWLER LOADER/3/4 YD/GENERAL/DSL "
});
</script>