0

Web サイトに入ったときに「Trace Window」ポップアップ ウィンドウを実装し、Web サイト全体でそのウィンドウにメッセージを送信して、サイトで発生している厄介な問題を診断しようとしています。問題は、新しい TraceText が追加される前に、トレース ウィンドウが既に存在する場合、ページが変更されることです。すべてのコンテンツが削除されます。私が欲しいのは、Web サイト内の任意のページからメッセージを送信できる Window です。

すべての画面にスクリプトとして含める JavaScript スクリプト debugger.js があります (以下を参照)。次に、sendToTraceWindow() 関数を呼び出して、Web サイト全体にメッセージを送信します。これは現在、現在調査中の問題のため、ほとんどが 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='trace' のテキストエリアのみ:

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

1 に答える 1

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);
}

新しいページが読み込まれ、トレース ウィンドウが更新されるたびに、既存のトレース データがローカル ストレージから読み込まれ、テキストエリアに表示されます。これにより、探している機能が実現するはずです。

エラーがあれば親切にしてください - 私は月曜日の朝にこれを飛ばしています!

jQuery ライブラリを見つけるのは簡単です。jStorage ライブラリはhttp://www.jstorage.info/で、jquery-json はhttp://code.google.com/p/jquery-json/で見つけることができます。

于 2012-05-28T16:43:53.650 に答える