Angular js,如何在调用url之前检查用户是否有Internet连接

时间:2015-01-05 09:11:06

标签: javascript angularjs

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

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

1 个答案:

答案 0 :(得分:-1)

    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