用Ajax异步调用一下链接地址!这段代码你看下 <script>//连接状态 <%--start连接状态--%> var xmlHttplink; function createXMLLinkHttpRequest() { if (window.ActiveXObject) { xmlHttplink = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttplink = new XMLHttpRequest(); } }
对AJax不太熟悉。
我的环境是一个客户端软件内嵌浏览器控件,加载html,想用js判断如果网络不通,就调用本地的html;如果网络连通,则调用服务器的html。这期间交互的都是纯html,不能用服务器端代码。如果能实现,请写一下代码吧,就实现js判断网络是否连通就行。谢谢
<script>//连接状态
<%--start连接状态--%>
var xmlHttplink;
function createXMLLinkHttpRequest() {
if (window.ActiveXObject) {
xmlHttplink = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttplink = new XMLHttpRequest();
}
}
function doRequest() {
createXMLLinkHttpRequest();
xmlHttplink.onreadystatechange = handleChange;
xmlHttplink.open("HEAD", "../../login.do?method=linkStatus&date=" + new Date().getTime(), true);
xmlHttplink.send(null);}function handleChange() {
if(xmlHttplink.readyState == 4) {
if(xmlHttplink.status == 200){
document.getElementById("netlink").value="连接";
}else{
document.getElementById("netlink").value="断开";
alert("连接断开");
}
setTimeout("doRequest()", 1000);
}
}
</script>
在页面加载的时候调用<body onload="doRequest();" >后台java代码:
//网络连接状态
public ActionForward linkStatus(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
return mapping.findForward("top");//直接跳回原页面就好
}
我这个不能用后台代码,只能用纯javascript
<img src="http://i1.sinaimg.cn/home/deco/2009/0330/logo_home.gif" onload="alert('能上')" onerror="alert("不能上");" />