用 onerror 擷取錯誤信

來源:互聯網
上載者:User
error|錯誤

  這個東西是線上寫 js 指令碼的時候,用來即時查錯的東西!

  也就是,當發現所編寫的指令碼有問題的時候會有相應的提示,並且在原始碼框中高亮標示出錯行,方便除錯!

<HTML><HEAD><TITLE>Debug Textarea</TITLE><meta http-equiv='Content-Type' content='text/html; charset=gb2312'></HEAD><style>* {font-size: 12px}body {margin: 10px; padding: 0px}table.list{cursor: default;border:1px solid #ccccccbackground-color: #cccccc;border-collapse: collapse;border-Color: #cccccc;}</style><script language="javascript">// Coded by windy_sk <windy_sk@126.com> 20040205function reportError(msg,url,line) {var str = "You have found an error as below: \n\n";str += "Err: " + msg + " on line: " + line;alert(str);return true;}window.onerror = reportError;document.onkeydown = function() {if(event.ctrlKey){switch(event.keyCode) {case 82: //rruncode();break;case 83: //ssavecode();break;case 71: //ggoto(prompt('Please input the line number', '1'));break;case 65: //adocument.execCommand("SelectAll");break;case 67: //cdocument.execCommand("Copy");break;case 88: //xdocument.execCommand("Cut");break;case 86: //vdocument.execCommand("Paste");break;case 90: //zdocument.execCommand("Undo");break;case 89: //ydocument.execCommand("Redo");break;default:break;}event.keyCode = 0;event.returnValue = false;}return;}function show_ln(){var txt_ln = document.getElementById('txt_ln');var txt_main = document.getElementById('txt_main');txt_ln.scrollTop = txt_main.scrollTop;while(txt_ln.scrollTop != txt_main.scrollTop) {txt_ln.value += (i++) + '\n';txt_ln.scrollTop = txt_main.scrollTop;}return;}function editTab(){var code, sel, tmp, r;event.returnValue = false;sel =event.srcElement.document.selection.createRange();r = event.srcElement.createTextRange();switch (event.keyCode){case (8):if (!(sel.getClientRects().length > 1)){event.returnValue = true;return;}code = sel.text;tmp = sel.duplicate();tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top);if(sel.parentElement() != tmp.parentElement()) return;;sel.setEndPoint('startToStart', tmp);sel.text = sel.text.replace(/^\t/gm, '');code = code.replace(/^\t/gm, '').replace(/\r\n/g, '\r');r.findText(code);r.select();break;case (9):if (sel.getClientRects().length > 1){code = sel.text;tmp = sel.duplicate();tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top);if(sel.parentElement() != tmp.parentElement()) return;sel.setEndPoint('startToStart', tmp);sel.text = '\t'+sel.text.replace(/\r\n/g, '\r\t');code = code.replace(/\r\n/g, '\r\t');r.findText(code);r.select();}else{sel.text = '\t';sel.select();}breakcase (13):tmp = sel.duplicate();tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top);if(sel.parentElement() != tmp.parentElement()) return;tmp.setEndPoint('endToEnd', sel);sel.text = '\r\n' + tmp.text.replace(tmp.text.replace(/^[\t ]+/g, ""),"");sel.select();break;default:event.returnValue = true;break;}return;}function runcode() {var str = document.getElementById("txt_main").value;var code_win = window.open('about:blank');code_win.document.open();code_win.document.writeln("<script>");code_win.document.writeln("function reportError(msg,url,line){\nline-=14;\nvar str='You have found an error as below: \\n\\n';\nstr+='Err: '+msg+' on line: '+(line);\nalert(str);\nopener.goto(line);\nopener.focus();\nwindow.onerror=null;\nsetTimeout('self.close()',10);\nreturn true;\n}");code_win.document.writeln("window.onerror = reportError;");code_win.document.writeln("<\/script>");code_win.document.writeln(str);code_win.document.close();return;}function savecode() {var str = document.getElementById("txt_main").value;var code_win = window.open('about:blank','_blank','top=10000');code_win.document.open();code_win.document.writeln(str);code_win.document.close();code_win.document.execCommand('saveas','','code.html');code_win.close();return;}function goto(ln) {if(!/^\d+$/.test(ln) || ln==0) return;var obj = document.getElementById("txt_main");var rng = obj.createTextRange();var arr = obj.value.split(/\n/);if(ln>arr.length) ln = arr.length;var str_tmp = "";for(var i=0; i<ln-1; i++) {str_tmp += arr[i];}rng.moveStart('character',str_tmp.length);str_tmp = "";for(i=ln; i<arr.length; i++) {str_tmp += arr[i];}rng.moveEnd('character',-str_tmp.length); rng.select();return;}window.onload = function() {document.getElementById("txt_main").value = "<script>\nalert(')\n<\/script>";}</script><BODY><table width='600' class='list' border='1' bgcolor='#eeeeee' bordercolorlight='#000000' bordercolordark='#FFFFFF' cellpadding='0' cellspacing='0'> <tr bgcolor='#cccccc'> <td colspan='2' height='20' align='center'><b>Debug Textarea</b></td> </tr> <tr> <td colspan='2'> <table width='95%' border='0' align='center' > <tr> <td align='center'><br /> <textarea id='txt_ln' name='content' rows='10' style='width:40px;overflow:hidden;height:200px;border-right:0px;text-align:right;line-height:14px' onselectstart='this.nextSibling.focus();return false' readonly></textarea><textarea id='txt_main' name='content' rows='10' cols='80' >   </td> </tr> </table> </td> </tr></table></BODY></HTML>



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。