2012-05-28 143 views
0

我试图在进入网站时实现'追踪窗口'弹出窗口,然后将消息发送到整个网站的窗口,以诊断我有一些更尴尬的问题与该网站。 问题是页面发生变化,如果跟踪窗口已经存在,则在添加新的TraceText之前删除所有内容。 我想要的是一个窗口,可以从网站内的任何页面发送消息。弹出窗口不会追加文字

我有一个JavaScript脚本debugger.js,我将其作为脚本包含在每个屏幕中(如下所示)然后我会调用sendToTraceWindow()函数在整个网站中向其发送消息。这是目前大部分在vbscript中完成,由于我正在调查的问题。 我认为这是因为我正在将debugger.js编写到每个屏幕中,它将traceWindow变量设置为null(请参阅下面的代码),但我不知道如何解决此问题!

任何帮助非常感谢。 安德鲁

代码示例:

debugger.js

var traceWindow = null 

function opentraceWindow() 
{ 
    traceWindow = window.open('traceWindow.asp','traceWindow','width=400,height=800') 
} 

function sendToTracewindow(sCaller, pMessage) 
{ 

    try 
    { 
     if (!traceWindow) 
     { 
     opentraceWindow() 
     } 

     if (!traceWindow.closed) 
     { 
     var currentTrace = traceWindow.document.getElementById('trace').value 
     var newTrace = sCaller + ":" + pMessage + "\n" + currentTrace 
     traceWindow.document.getElementById('trace').value = newTrace 
     } 
    } 
    catch(e) 
    { 
     var currentTrace = traceWindow.document.getElementById('trace').value 
     var newTrace = "error tracing:" + e.message + "\n" + currentTrace 
     traceWindow.document.getElementById('trace').value = newTrace 
    } 

} 

traceWindow.asp - 只是一个ID = '追踪' 的textarea:

<HTML> 
    <head> 
     <title>Debug Window</title> 
    </head> 
    <body> 
     <textarea id="trace" rows="50" cols="50"></textarea> 
    </body> 
</HTML> 
+0

你想再次追踪什么?要诊断哪种类型的错误? –

+0

不是一定的错误,但任何我可能会觉得有用的跟踪:)。例如,我可能想在TraceWindow中显示String sStatus是什么时候的特定时间,所以可能会调用sendToTraceWindow(“window_onmousemove”,iStatus)或者当某人按下'Save'按钮时,我会记录That traceWindow sendToTraceWindow(“save_mouseup”,“保存按钮按下”)。所以不是错误 - 只是我可能发现错误的文本。 –

回答

1

我不要认为你的traceWindow变量会被重置在每个页面加载,因此呈现您的句柄到现有的窗口无效。但是,如果您不介意利用LocalStorage和一些jQuery,我相信您可以实现您正在寻找的功能。

您的跟踪窗口改成这样:

<html> 
    <head> 
     <title>Debug Window</title> 
     <script type="text/javascript" src="YOUR_PATH_TO/jQuery.js" /> 
     <script type="text/javascript" src="YOUR_PATH_TO/jStorage.js" /> 
     <script type="text/javascript" src="YOUR_PATH_TO/jquery.json-2.2.js" /> 
     <script type="text/javascript"> 
     var traceOutput; 
     var traceLines = []; 
     var localStorageKey = "traceStorage"; 

     $(function() { 
      // document.ready. 
      // Assign the trace textarea to the global handle. 
      traceOutput = $("#trace"); 
      // load any cached trace lines from local storage 
      if($.jStorage.get(localStorageKey, null) != null) { 
      // fill the lines array 
      traceLines = $.jStorage.get(localStorageKey); 
      // populate the textarea 
      traceOutput.val(traceLines.join("\n")); 
      } 
     }); 

     function AddToTrace(data) { 
      // append the new trace data to the textarea with a line break. 
      traceOutput.val(traceOutput.val() + "\n" + data); 
      // add the data to the lines array 
      traceLines[tracelines.length] = data; 
      // save to local storage 
      $.jStorage.set(localStorageKey, traceLines); 
     } 

     function ClearTrace() { 
      // empty the textarea 
      traceOutput.val(""); 
      // clear local storage 
      $.jStorage.deleteKey(localStorageKey); 
     } 
     </script> 
    </head> 
    <body> 
     <textarea id="trace" rows="50" cols="50"></textarea> 
    </body> 
</html> 

而且,在你想跟踪数据您的网页,你可以修改你的JavaScript像这样:

var traceWindow = null;     
function opentraceWindow() {      
    traceWindow = window.open('traceWindow.asp','traceWindow','width=400,height=800'); 
} 

function sendToTracewindow(sCaller, pMessage) { 
    traceWindow.AddToTrace(sCaller + ":" + pMessage); 
} 

每当一个新的页面被加载并且跟踪窗口被刷新,现有的跟踪数据从本地存储中加载并显示在你的textarea中。这应该实现您正在寻找的功能。

请善待任何错误 - 我在星期一早上收到这一点!

寻找jQuery库应该是微不足道的。你可以在这里找到jStorage库:http://www.jstorage.info/,你可以在这里找到jquery-json:http://code.google.com/p/jquery-json/

+0

非常好谢谢布拉德。它只会在开发环境中“开启”,所以我对本地存储没有任何问题。看起来像一个很好的解决方案。我之前没有使用jquery,所以我会放弃它! –