Javascript 如何在调用url之前检查用户是否连接了internet

Javascript 如何在调用url之前检查用户是否连接了internet,javascript,angularjs,Javascript,Angularjs,在调用myurl之前,我需要检查Internet连接 $scope.clicked=function(id){ if($window.navigator.onLine){ $window.open('myurl='+id); }else{ alert('offline'); } }; 为什么不处理一个404错误?@Davin Tryon你能解释一下你的实际问题吗?每次我试图打电话给我的网络服务器时,我都需要检查互

在调用myurl之前,我需要检查Internet连接

$scope.clicked=function(id){
 if($window.navigator.onLine){ 
         $window.open('myurl='+id);
         }else{
            alert('offline');
         }
};

为什么不处理一个404错误?@Davin Tryon你能解释一下你的实际问题吗?每次我试图打电话给我的网络服务器时,我都需要检查互联网连接可能是重复的Hallo Ankit,我正在用AngularJS开发一个移动应用程序,问题是,我需要检查互联网是否可用,必须适用于所有浏览器
    You can use window.navigator.onLine but no need to write $ before window.
 It works fine if you have only one network connection but it shows online even if you are not connected to internet but with any internal network like loopback adapter too. So best option to check connectivity with internet is html5 Web Socket but itonly works on windows 8 so if you don't have windows 8 then you can use html5 EventSource interface. you can read about it here https://developer.mozilla.org/en-US/docs/Web/API/EventSource