如何通过从服务器调用 gif 来检查是否存在互联网连接?

how can i check if there is a connection internet connection by calling a gif from a server?

我正在开发 phonegap 应用程序,我想测试是否有连接互联网显示 iframe,否则显示消息 "please check your internet connection"。 我使用了 javascript 代码,它在导航器上对我有用,但是当我构建应用程序时它对我不起作用,我的代码是这样的:

<div id="app"></div>
<script type="text/javascript">
var online = navigator.onLine;
if (online == true) {
  $( "div#app" ).html("<iframe src='#' frameborder=0 width=100%></iframe>");
} else {
  $( "div#app" ).html('NO INTERNET CONNEXION');
} 
</script>

还有其他方法可以使用插件检查 cordova/phonegap 的互联网访问,但如果您想检查图像是否可用,那么您始终可以检查其在缓存中的大小。

这些解决方案中的任何一个都应该引导您前进:

Determining image file size + dimensions via Javascript?

或其他可能的答案

如果您检查图像,请确保将参数时间戳附加到 url... 因此...调用 http://www.myserver.com/img/blank.gif?x=1122334455

这将确保您的测试不会 return 出现误报,如果图像 URL 之前缓存在客户端设备中,就会出现这种情况。