我想全局使用check变量。因为我想将此文件包含在另一个文件中,并希望对保存位置的变量应用条件。但我无法在全球范围内使用它。请帮助我
<script language="javascript">
var check= "initial";
function fun() {
function displayLocation(latitude, longitude) {
var request = new XMLHttpRequest();
var method = 'GET';
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';
var async = true;
request.open(method, url, async);
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
var data = JSON.parse(request.responseText);
var address = data.results[1];
check = address.formatted_address;
alert(check);
}
};
request.send();
};
var successCallback = function (position) {
var x = position.coords.latitude;
var y = position.coords.longitude;
displayLocation(x, y);
};
var errorCallback = function (error) {
var errorMessage = 'Unknown error';
switch (error.code) {
case 1:
errorMessage = 'Permission denied';
break;
case 2:
errorMessage = 'Position unavailable';
break;
case 3:
errorMessage = 'Timeout';
break;
}
alert(errorMessage);
};
var options = {
enableHighAccuracy: true,
timeout: 1000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options);
}
fun();
document.write(check);
</script>