`

简陋的Javascript脚本调试工具

阅读更多
  现在“富客户端”是炒得比较火的一个概念。所谓的富客户端一般需要写大量的javascript/vbscript代码,脚本语言是比较难调试的,虽然可以使用OFFICE中带的脚本调试程序、DOTNET或其它的专业工具来调试,可总是些不方便。
  写过VC程序的人相信比较熟悉TRACE、afxDump等几个函数,这几个函数可以在工具窗口实时的输出一些调试信息,可以很方便的发现一些运行时错误。有人使用纯脚本的方式实现了类似的跟踪调试功能,经过使用发现确实可以给开发带来比较大的方便。代码是以CodeProject网站上找到的,原理很简单,使用很方便。调试信息分为Message、Warn及Exception几种,以不同的颜色显示,很直观。
  下面把相应代码及使用帮助贴出来,感兴趣的网友可以拷贝粘贴后使用。
  主要是两个文件:
/***************************************************************************/
  一、脚本文件(文件名:debuggingTools.js)
/***************************************************************************/
//debug helper class to control popup windows
var DebugHelper = function()
{
    this.Active = true;
    this.ShowException = true;
    this.ShowURL = true;
    this.ShowLastModified = false;
    this.ShowReferrer = false;
    this.VerboseMode = false;
    //reference to the popup window
    this.DebugWindow = null;
    this.CssStyleFile = new String("debugWindow.css");
    this.WindowStyle = new String("left=0,top=0,width=300,height=300,scrollbars=yes,status=no,resizable=yes");
    //no spaces to run correctly on internet explorer
    this.WindowName = new String("JavascriptDebugWindow");
    this.WindowTitle = new String("Javascript Debug Window");
}

//method to show the debug window
DebugHelper.prototype.ShowWindow = function()
{
 try
 {
     if( this.Active )
     {
      this.DebugWindow = window.open("", this.WindowName, this.WindowStyle);
        this.DebugWindow.opener = window;
        //open the document for writing
        this.DebugWindow.document.open();
        this.DebugWindow.document.write(
          "<html><head><title>" + this.WindowTitle + "</title>" +
          "<link rel='stylesheet' type='text/css' href='" + this.CssStyleFile + "' />" +
          "</head><body><div id='renderSurface' style='width: 100%; height: 100%;' /></body></html>\n"
         );
         this.DebugWindow.document.close();
     }
 }
 catch(ex)
 {
  //ignore exception
 }
}  

//if the debug window exists, then write to it
DebugHelper.prototype.$Write = function(cssClass, message, url, lastModified, referrer)
{
 try
 {
  if( this.Active )
  {
   if( this.DebugWindow && ! this.DebugWindow.closed )
   {
       var msg = message;
      
       if( this.ShowURL && url != null )
           msg += " at " + url;
          
       if( this.ShowLastModified && lastModified != null )
           msg += " last modified in " + lastModified;

       if( this.ShowReferrer && referrer != null )
           msg += " referrer " + referrer;

       this.DebugWindow.document.getElementById("renderSurface").innerHTML = "<span class='" + cssClass + "'>" + msg + "</span>" + this.DebugWindow.document.getElementById("renderSurface").innerHTML;
     }
  }
 }
 catch(ex)
 {
  //ignore exception
 }
}

//write a message to debug window
DebugHelper.prototype.Message = function(message, url, lastModified, referrer)
{
 try
 {
     this.$Write("debugMessage", message, url, lastModified, referrer);
 }
 catch(ex)
 {
  //ignore exception
 }
}

//same as debug, plus another style applyied
DebugHelper.prototype.Warn = function(message, url, lastModified, referrer)
{
 try
 {
     this.$Write("debugWarn", message, url, lastModified, referrer);
 }
 catch(ex)
 {
  //ignore exception
 }
}

//same as debug, plus a strong style applyied
DebugHelper.prototype.Exception = function(message, url, lastModified, referrer)
{
 try
 {
     if( this.ShowException )
     {
         this.$Write("debugException", message, url, lastModified, referrer);
     }
 }
 catch(ex)
 {
  //ignore exception
 }
}

//if the debug window exists, then close it
DebugHelper.prototype.HideWindow = function()
{
 try
 {
     if( this.DebugWindow && !this.DebugWindow.closed )
     {
      this.DebugWindow.close();
         this.DebugWindow = null;
       }
 }
 catch(ex)
 {
  //ignore exception
 }
}

//create a global debug object
var debugHelper = new DebugHelper();
//you should show the window right here to get loading errors or sintax errors of other pages
//debugHelper.ShowWindow();

//catch generic errors also
function WindowOnError(msg, url, line)
{
 if( debugHelper )
 {
  debugHelper.Exception(msg, line + " at " + url);
 }
}
window.onerror = WindowOnError;
/***************************************************************************/
  二、样式表(文件名:debugWindow.css)
/***************************************************************************/
body
{
 background-color: #ffffff;
 font-family: "Courier New", Courier, monospace;
}
span.debugMessage
{
 border-bottom:1px dotted #cccccc;
 color: #000000;
 display: block;
 margin: 1px;
}
span.debugWarn
{
 border-bottom:1px dotted #aaaa00;
 color: #0000aa;
 display: block;
}
span.debugException
{
 border-bottom:1px dotted #ff0000;
 color: #aa0000;
 display: block;
 font-weight: bold;
}
/***************************************************************************/
  三、使用示例
/***************************************************************************/
  使用很简单了,在网页上包含上面的脚本文件,使用下面几个函数就可以了。
 debugHelper.ShowWindow();//显示调试窗口
 debugHelper.HideWindow();//隐藏调试窗口
  debugHelper.Message("信息");//显示message级别信息
 debugHelper.Warn("信息");//显示warn级别信息。
 debugHelper.Exception("信息");//显示Exception级别信息。

/***********本人原创,欢迎转载,转载请保留本人信息*************/
作者:wallimn
电邮:wallimn@sohu.com
博客:http://wallimn.iteye.com  http://blog.csdn.net/wallimn
时间:2006-11-15
/***********本人原创,欢迎转载,转载请保留本人信息*************/
分享到:
评论

相关推荐

    javascript脚本调试工具

    javascript脚本调试工具 此工具用了 MSScriptControl 微软的脚本控件 有BUG或者建议请留言...... 邮箱:ybhacker@qq.com 运行环境:.net4.0

    javascript脚本调试工具Microsoft Script Debugger:

    javascript脚本调试工具 Script Debugger 集成在IE中的一款很原始的调试工具,具备基本的调试功能,除了可以用于调试客户端脚本,还能调试在Microsoft IIS上运行的服务器端脚本。该工具命令窗口是基于文本的,针对...

    利用ie进行javascript脚本调试用vs.net调试javascript

    打开ie-&gt;工具菜单-&gt;inter选项-&gt;高级选项卡-&gt;去掉“禁止脚本调试"选项 2,打开vs.net,创建一个新的asp.net项目(或打开一个) 3,运行你要调试的页面 4,当你的页面呈现在ie里的时候,重新回到vs.net 5,在脚本中设置断点:

    支持JavaScript的串口调试工具(免费)

    支持JavaScript的串口调试工具。 可以在收到数据的时候调用自定义的JavaScript脚本。可以用脚本对收到数据进行读取、向指定串口发送字符串。 如果发现Bug或有建议请发邮件到 ke_dong#126.com

    一款好用的可编程串口调试工具 支持JS 脚本编程

    工欲善其事 必先利其器,可编程的串口专业工具,你想象不到的强大和简单 1、普通串口助手功能 2、可编程脚本运行模式,多脚本支持,多demo可参考,提供脚本编写服务 3、常用的CRC校验支持 应用场景:标准调试场景,...

    Internet Explorer 的JavaScript调试工具

    安装后,打开IE,点工具菜单,选Internet选项,再点高级选项卡,在里面去掉禁用脚本调试(Internet Explorer)的小勾,然后一路确定,最后关了IE,重开。当遇到网页JS有错,则会显示出出错的是哪行。方便开发人员调试...

    java script 调试工具 脚本控制台

    2,通过控制台查看目的网页的DOM文档树,执行JavaScript脚本, 界面介绍: 1, 命令历史窗口:保存命令的历史 2, 命令窗口 直接输入javascript语句,如:document.title; 将返回被调试页面的页面标题; ...

    最有用js调试工具.rar

    有史以来最实用的js调试工具,javaScript脚本调试方便、快速、简单,直接安装,并按照安装文件进行设置即可。

    《JavaScript脚本特效编程给力起飞》

    项目研发过程中的测试和开发是密不可分的,因此本巴士还在最后张贴了Web测试的知识,引入了网页调试工具Firebug,并介绍了它的使用,让你不仅能够掌握JavaScript的知识,还能学到网页测试方面的内容,从而全面提高...

    Pascal和JavaScript的脚本设计工具

    ◎支持大部分的Delphi7/Pascal/JavaScript语法(见下述说明) ◎超过 50 个内置单元库 ◎超过 1000 个 函数/过程 ◎超过 250 个 classes ◎直接支持超过 110 个 Components ◎直接装载DFM并将相关事件绑定脚本函数 ◎...

    Javascript调试工具(下载)

    论坛上有人问javascript Debug的工具,在这里就推荐两个javascript的调试工具给大家,今后我也会说一下调试的方法。IE下推荐的调试工具就是VS studio了,这个的下载地址我就不给出了,相信大家都应该有。基本的调试...

    JavaScript css浏览器的调试

    1.对IE浏览器的调试 2 1.1. IE6,IE7浏览器对JS的调试方式 2 1.1.1准备工作 2 1.1.2调试脚本步骤 3 1.1.3总结 4 1.2.IE8浏览器对JS的调试方法 4 1.2.1.准备工作 4 1.2.2调试脚本步骤 5 ...5.Opera浏览器调试工具 35

    ie脚本调试器.rar

    ie 脚本 调试器 在ie环境下的javascript调试工具,不可多得,下载了还得感谢我

    IE javascript 调试工具

    FullSource 能显示javascript动态生成的DOM代码,特别对学习EXTJS 的人有帮助,ScriptDebugger 就是IE调试脚本的利器

    IE js调试工具使用

    在ie中查看所编写页面js出现的错误。安装.exe后重启IE 在查看-&gt;浏览器栏-&gt;Companion.js打开即可。若不能打开则将 工具-&gt;internet选项-&gt;高级之下的两项禁止脚本调试之前的勾去掉

    一个IE下的优秀js调试工具(附安装步骤)

    一个IE下的优秀js调试工具,做web开发的朋友都清楚,js程序的调试是相当郁闷的,因为首先这种语言语法比较灵活,它是一种弱类型的脚本语言,很多错误是无法控制的,这些不谈,最 痛苦的是没有什么好的调试工具,现在...

    java script 调试器

    非常不错的javascript脚本调试工具,安装即可使用

    非常实用的JavaScript调试工具

    一个非常实用的JavaScript 脚本编辑软件,有着丰富的代码编辑功能(JavaScript, HTML, CSS, VBScript, PHP ,ASP(Net)语法加亮),内置预览功能,提供了完整的HTML 标记, HTML 属性, HTML 事件, JavaScript 事件和...

    微软的javascript调试工具

    在官方网站不好找,放这里来方便更多人。 Internet选项里面,去掉“禁止脚本调试”选项。js出错时就会提示调试了。

    脚本控制台 甲子版 通过控制台查看目的网页的DOM文档树,执行JavaScript脚本

    2,通过控制台查看目的网页的DOM文档树,执行JavaScript脚本, 界面介绍: 1, 命令历史窗口:保存命令的历史 2, 命令窗口 直接输入javascript语句,如:document.title; 将返回被调试页面的页面标题; ...

Global site tag (gtag.js) - Google Analytics