<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
if(navigator.geolocation){
alert('yes,yes')
navigator.geolocation.getCurrentPosition(locationSuccess,locationError,{
// 指示浏览器获取高精度的位置,默认为false
enableHighAcuracy: true,
// 指定获取地理位置的超时时间,默认不限时,单位为毫秒
timeout: 5000,
// 最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
maximumAge: 3000
})
}else{
alert('nono')
}
locationError: function(error){
switch(error.code) {
case error.TIMEOUT:
showError("A timeout occured! Please try again!");
break;
case error.POSITION_UNAVAILABLE:
showError('We can\'t detect your location. Sorry!');
break;
case error.PERMISSION_DENIED:
showError('Please allow geolocation access for this to work.');
break;
case error.UNKNOWN_ERROR:
showError('An unknown error occured!');
break;
}
}
locationSuccess:function(position){
var coords = position.coords
alert(coords.latitude)
}
</script>
</head>
<body>
</body>
</html>
不知道爲什麼被踩,答案已刪除。
公子已更正自己的答案,內容與我原先的答案一致。
label 写法冒号后面接的是代码语句,匿名函数只能存在于语句中,而不能单独作为语句运行。
name: function() {
}
一般是对象中的表达式写法,一般对象声明属性或函数时,有以下两种方式:
function obj1() {
a: 1,
b: function() {
xxxx
}
}
function obj2() {
this.a = 1;
this.b = function() {
xxx
}
}
你此处若要定义函数,直接如下写即可:
function locationError() {
xxx
}
function locationSuccess() {
xxx
}
推荐你本书javascript高级程序设计,这里从语法到一些原理都有,不错的!