用 onerror 获取错误信息

2008-02-23 06:21:10来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

这个东西是在线写 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 #cccccc
background-color: #cccccc;
border-collapse: collapse;
border-Color: #cccccc;
}
</style>
<script language="javascript">
// Coded by windy_sk <windy_sk@126.com> 20040205

function 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: //r
runcode();
break;
case 83: //s
savecode();
break;
case 71: //g
goto(prompt('Please input the line number', '1'));
break;
case 65: //a
document.execCommand("SelectAll");
break;
case 67: //c
document.execCommand("Copy");
break;
case 88: //x
document.execCommand("Cut");
break;
case 86: //v
document.execCommand("Paste");
break;
case 90: //z
document.execCommand("Undo");
break;
case 89: //y
document.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();
}
break
case (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");

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: UBB 转换函数演示

下一篇: 在网页调用 WINDOWS 控件两例