如果没有互联网连接,请隐藏iframe
if there is no internet connection, hide iframe
iframe:
<iframe id="iframe" title="Environment Canada Weather" src="" allowtransparency="false" frameborder="0" height="170"></iframe>
jquery:
$( document ).ready(function() {
window.setInterval(function(){
if (navigator.onLine) {
//$("#iframe").show();
$("#iframe").attr("src", "http://weather.gc.ca/wxlink/wxlink.html?cityCode=on-143&lang=e");
}
else{
$("#iframe").hide();
}
}, 5000);
});
如果没有互联网连接,我无法隐藏iframe。我不知道这里出了什么问题。谢谢
navigator.onLine
告诉浏览器是否处于"脱机"模式。
它实际上并没有像你想象的那样检查你是否能触网。
(要做到这一点,你可以尝试用ajax ping Google.com,或者做一些类似的技巧)
您可以使用type:'jsonp'
和timeout:3000
(即三秒)通过框架对要包含的同一页面进行ajax请求。
$.ajax({
type : "GET",
url : "http://weather.gc.ca/wxlink/wxlink.html?cityCode=on-143&lang=e",
timeout : 3000,
dataType : "jsonp",
crossDomain : true,
success : function (response, textS, xhr) {
// never get here
},
error : function (xmlHttpRequest, textStatus, errorThrown) {
if (textStatus === 'timeout') {
// not reachable
} else {
// reachable
}
}
});
然而,您总是会遇到语法错误,因为该页面的内容不是脚本。但浏览器通常能够在出现此类错误后进行恢复。UPD。只是用你的网址试了一下,就行了。
相关文章:
- 在jquery中部分隐藏iframe
- 如果未正确加载,则隐藏iframe
- 单击后如何隐藏iframe
- 如果没有互联网连接,请隐藏iframe
- 如何在WordPress页面模板中隐藏iframe的元素
- 如何隐藏 Iframe src
- 当 iframe 没有 ID 时,对父级隐藏 iframe 中的元素
- 如何隐藏iframe src
- 隐藏iframe-隐藏时停止其中的脚本
- 如何在隐藏iframe时暂停YouTube播放器
- 如何使用Python Selenium单击隐藏iframe中的元素
- 隐藏iframe chrome回调未触发
- 使用隐藏iframe提交数据
- 隐藏Iframe刷新Ajax选项
- 如果内容为空,如何隐藏iframe
- 打开隐藏iframe下载文件
- PHP隐藏iframe直到特定日期
- 打印pdf隐藏iframe javascript/jquery c# MVC
- 隐藏iframe直到链接被点击,然后在链接被点击后隐藏链接
- 当元素单击时隐藏iframe