检查是否在网上和一个特定的网站都在的JavaScript / AJAX [英] Checking if the Internet and a particular site are up in JavaScript/AJAX

查看:96
本文介绍了检查是否在网上和一个特定的网站都在的JavaScript / AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它运行在本地设备上一个页面,我想用AJAX或者普通的JavaScript,检查设备是否连接到Internet 如果某个网站就到了。

I have a page which runs locally on my device, and I would like to use AJAX or normal JavaScript to check if the device is connected to the Internet and if a particular site is up.

此外,我试图找出如何指望通过AJAX一个RSS feed条目数,但我从来没有围绕RSS之前编程。这是不一样大的一个重点,但。

Additionally, I'm trying to figure out how to count the number of entries in an RSS feed through AJAX, but I've never programmed around RSS before. This isn't as big of a priority, though.

推荐答案

与阿贾克斯,脚本和图像的加载不受同源策略,这样你就可以查询自己的存在(但不能读取其内容)跨域。如果你知道一个特定图像的网站上,你可以使用的onload 新的图像事件处理程序,如果测试一个网站是由:

Unlike Ajax, the loading of scripts and images is not subject to the same-origin policy, so you can query for their existence (but not read their contents) cross-domain. If you know a particular image on the site, you can use the onload event handler of a new Image to test if a site is up:

var i = new Image();
i.onload = function() { alert("site is up!") }
i.onerror = function() { alert("site is not up!") }
i.src = "/favicon.ico";

/favicon.ico 是一个巨大的各大网站的数量,因为它需要在较旧的IE图标的功能。网络的并非每个站点都有它(事实上,并不是每一个网站的网页上有一个图标),但它会为各大网站,如谷歌,维基百科,堆栈溢出等。

/favicon.ico is on a huge number of major sites, since it's required for favicon functionality in older IE. Not every site of the Web has it (indeed, not every site on the Web has a favicon), but it will work for major sites like Google, Wikipedia, Stack Overflow, etc.

/favicon.ico 是很好的跨站点的支持,但如果你只是要针对一个特定的网站,只要发现在该网站上,查询图像的存在。

/favicon.ico is nice for cross-site support, but if you only want to target one specific site, simply find an image on that site and query for its existence.

这篇关于检查是否在网上和一个特定的网站都在的JavaScript / AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆