如何检测js中元素导航的状态

How to detect the status of the element navigation in js?

本文关键字:元素 导航 状态 js 何检测 检测      更新时间:2023-09-26

我需要从浏览器中获取用户的纬度和经度,但在大多数浏览器上都有一些限制。如何告诉用户他的地理位置已关闭或根本不支持?

我试过这样的东西,但无论是ipad还是mac上的safari都没有提示任何东西。

if (navigator.geolocation)
    navigator.geolocation.getCurrentPosition(success);
else
    alert('not supported');

演示:http://jsfiddle.net/7sRdS/

if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) {  
        // ... 
    }, function(error) {
        //error handling
        switch(error.code) {
            case error.PERMISSION_DENIED:
              //User denied the request for Geolocation.             
              break;
            case error.POSITION_UNAVAILABLE:
              //Location information is unavailable.
              break;
            case error.TIMEOUT:
              //The request to get user location timed out.
              break;
            case error.UNKNOWN_ERROR:
              //An unknown error occurred.
              break;
        }
        alert("Geolocation error: " + error.code);
    },
    {
        timeout:10000 //10s
    });
} else {
    alert("Geolocation services are not supported by your browser.");
} 

看看modernizr,它提供了多个特征检测测试。