Jquery/Ajax Code which check the internet [duplicate]

霸气de小男生 提交于 2020-01-22 03:20:12

问题


Jquery Code which check the internet/network is there or not(mobile/PC/Tablet).It must just check on page load.I thinkAjax will good because Ajax will check after certain interval.

I am looking just like http://tomriley.net/, But it is plugin, I am looking for simple jquery/Javascript.

Its static page which check system internet only.

Any idea is appreciated.


回答1:


You might try a $.ajax() invoication with a .fail() handler, for example JQuery's getJSON():

var network_available;         // bool
var url = '/some/json/call';   // must be relative to the site that 
                               // you are already addressing

$.getJSON(url, function(data) {
    network_available = true;
})
.fail(function() {
    network_available = false;
});

Though I doubt this will solve all of your problems. The Javascript engine won't allow 'foreign' URL's, just the domain that the script or page was received from. So you'd not be really testing network availability, but also whether your site is up and responding within a reasonable time.



来源:https://stackoverflow.com/questions/27205742/jquery-code-to-check-internet-connection-and-change-the-button-as-per-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!