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

Angular js , How to check if user has internet connection, before calling url

本文关键字:用户 检查 是否 连接 互联网 url js 调用 Angular      更新时间:2023-09-26

我需要在调用myurl之前检查互联网连接

$scope.clicked=function(id){
 if($window.navigator.onLine){ 
         $window.open('myurl='+id);
         }else{
            alert('offline');
         }
};
    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