开发网页程序的时候,JS经常报错该如何处理?
来源:互联网 发布:java捕获异常并抛出 编辑:程序博客网 时间:2024/05/01 02:27
做网页的时候,JS一段没有写好的时候,老是会出错,弹出的窗口又难看,而且在IE里面,还死活找不到位置。。。因为IE的报错信息位置不准确,再加上,有时候一段网页是专门为IE写的,用FF或者OPERA就会报错,为避免报错信息弹窗,让客户体验更好,因此。。。找了一些资料后,发现以下解决方法比较好。
1、在代码前加入
<script language="javascript">window.onerror = function(){return true;}</script>
把这段代码放在你的head的JS引用前
2、在具体的代码前加入try catch
<script language="javascript">
try{
可能会出错的代码
}
catch(e){}
</script>
3、自己写个小函数替代(COPY 自网上)
document._error_messages = new Array();
var w;
function doError(msg,url,ln) {
var _error_obj = {msg : msg, url : url, ln : ln};
document._error_messages[document._error_messages.length] = _error_obj;
str = ""
str += "<title>Error Dialogue (WebFX)</title>"
str += "<script>window.onload=new Function('showError()');"
str += 'var nr=0;'
str += 'function next() {'
str += ' nr=Math.min(window.opener.document._error_messages.length-1,nr+1);'
str += ' showError();'
str += '}'
str += 'function previous() {'
str += ' nr=Math.max(0,nr-1);'
str += ' showError();'
str += '}'
str += 'function showError() {'
str += ' errorArray = window.opener.document._error_messages;'
str += ' if (errorArray.length != 0 && nr >= 0 && nr < errorArray.length) {'
str += ' url.innerText = errorArray[nr].url;'
str += ' msg.innerText = errorArray[nr].msg;'
str += ' ln.innerText = errorArray[nr].ln;'
str += ' }'
str += '}</script>'
str += "<style>"
str += "body {background: white; color: black; border: 10 solid navy; font-family: tahoma, arial, helvitica; font-size: 12px; margin: 0;}"
str += "p {font-family: tahoma, arial, helvitica; font-size: 12px; margin-left: 10px; margin-right: 10px;}"
str += "h1 {font-family: arial black; font-style: italic; margin-bottom: -15; margin-left: 10; color:navy}"
str += "button {margin: 0; border: 1 solid #dddddd; background: #eeeeee; color: black; font-family: tahoma, arial; width: 100}"
str += "a {color: navy;}"
str += "a:hover {color: blue;}"
str += "</style>"
str += '<body scroll="no">'
str += "<h1>Oops!</h1>"
str += '<p>An error ocurred in the file <br><strong id="url"></strong><br>This might prevent the page from working correctly.</p>'
str += '<p style="margin-bottom: 5;">Please report this to <a href="/contact.html#emil">EAE</a> or <a href="/contact.html#erik">Erik</a></p>'
str += '<table style="width: 100%;" cellspacing=0 cellpadding=10><tr><td>'
str += '<button onclick=/'if (infoArea.style.display!="block") {infoArea.style.display = "block";window.resizeTo(400,308);this.innerText="Hide Error";}else {infoArea.style.display="none";window.resizeTo(400,219);this.innerText="Show Error";}/''
str += 'onmouseover="this.style.borderColor=/'black/'" onmouseout="this.style.borderColor=/'#dddddd/'">Show Error</button>'
str += '</td><td align="RIGHT"><button onclick="window.close()" onmouseover="this.style.borderColor=/'black/'" onmouseout="this.style.borderColor=/'#dddddd/'">Ok</button>'
str += '</td></tr></table>'
str += '<div id="infoArea" style="display: none;">'
str += '<div id="info" style="background: #eeeeee; margin: 10; margin-bottom: 0; border: 1 solid #dddddd;">'
str += '<table>'
str += '<tr><td><p>Message:</p></td><td><p id="msg"></p></td></tr>'
str += '<tr><td><p>Line:</p></td><td><p id="ln"></p></td></tr>'
str += '</table>'
str += '</div>'
str += '<table style="width: 100%;" cellspacing=0 cellpadding=10><tr><td>'
str += '<button onclick="previous()" onmouseover="this.style.borderColor=/'black/'" onmouseout="this.style.borderColor=/'#dddddd/'">Previous</button>'
str += '</td><td align=right><button onclick="next()" onmouseover="this.style.borderColor=/'black/'" onmouseout="this.style.borderColor=/'#dddddd/'">Next</button>'
str += '</td></tr></table>'
str += '</div>'
str += '</body>'
if (!w || w.closed) {
w = window.open("","_webxf_error_win","width=390,height=190");
var d = w.document;
d.open();
d.write(str);
d.close();
w.focus();
}
return true;
}
window.onerror = doError
- 开发网页程序的时候,JS经常报错该如何处理?
- 开发网页程序的时候,JS经常报错该如何处理?
- Eclipse开发程序的时候,经常要用到第三方jar包
- 网页中经常用到的JS脚本
- 用cvSaveImage的时候,经常报错:error C2198: 'cvSaveImage' : too few arguments for call
- Android studio gradle构建工程的时候很慢经常报错解决方案
- 网页前端开发经常会逛到的网站
- 开发中经常tomcat启动报错
- 请求jquery.min.js的时候,log显示浏览器还请求了jquery.min.map,该请求导致服务端报错
- 在onfancy index 价格时候报错的处理
- 【分享】android开发该如何提高Android程序的效率
- 【分享】android开发该如何提高Android程序的效率
- android开发该如何提高Android程序的效率
- android开发该如何提高Android程序的效率
- 运行ASP.NET的程序的时候IIS报错
- 【网络开发】组播的时候到底该如何绑定网卡
- Delphi XE8中开发DataSnap程序常见问题和解决方法 (-)启动创建好的DBExpress工程时候报错了!
- android开发时候的一个奇怪的报错
- PHP常见问题汇总
- 对 "闭包-closure" 的一些见解http://wj.cnblogs.com/archive/2006/04/22/381851.html
- 华为收购港湾核心业务 有恶意收购嫌疑
- 广州番禺大夫山森林公园游记
- 在Javascript中,什么是闭包(Closure)
- 开发网页程序的时候,JS经常报错该如何处理?
- 第五次作业
- ubuntu下开机自动运行脚本以及定时任务的处理
- 如何取得当前数据的上一条和下一条的ID
- Flex 应用展示之 BEST4C 在线绘图工具
- TOMCAT下基于表单验证常见问题:(oracle)
- 怎样保持键盘清洁
- 自动化测试的计划和实施总纲
- CSDN Blog推出专属的离线发布工具 - CSDN剪影