检查互联网的 Jquery/Ajax 代码

2022-01-11 00:00:00 jquery jquery-plugins javascript html

检查互联网/网络是否存在的Jquery代码(手机/PC/平板电脑).它必须只检查页面加载.我认为Ajax会很好,因为Ajax会在一定间隔后检查.

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.

我看起来就像 http://tomriley.net/,但它是插件,我正在寻找简单的 jquery/Javascript.

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.

任何想法都值得赞赏.

推荐答案

您可以尝试使用 .fail() 处理程序的 $.ajax() 调用,例如例如 JQuery 的 getJSON():

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;
});

虽然我怀疑这会解决你所有的问题.Javascript 引擎不允许外来"URL,只允许接收脚本或页面的域.因此,您不会真正测试网络可用性,而是测试您的网站是否已启动并在合理的时间内响应.

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.

相关文章