Posted By

wizard04 on 06/17/08


Tagged

javascript object window popup debug console alert breakpoint


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

SpinZ
mifly


Debug object


 / Published in: JavaScript
 

An object including a few helpful functions for debugging. Functions displaying popups or alerts are limited to a certain number of consecutive times before asking to continue, in case they end up being called in an endless loop or something.

  1. if(!window.config) var config = {};
  2. config.Debug = {
  3. maxPopups: 5 //default number of popups/alerts allowed before asking to continue
  4. };
  5.  
  6. //static Debug object
  7. var Debug = function()
  8. {
  9. //***** private properties and methods *****//
  10.  
  11. var maxPopups = window.config.Debug.maxPopups;
  12. var debugWindow;
  13. var popupCount = 0;
  14. var continuePopups = true;
  15.  
  16. //write a message (HTML) to the debug window
  17. function write(msg)
  18. {
  19. if(!debugWindow || !debugWindow.document)
  20. {
  21. debugWindow = window.open("", "debug"); //create the debug window
  22. if(!debugWindow) //a popup blocker caught it
  23. {
  24. msg = msg.replace(/<br(\s+\/)?>/ig, "\n");
  25. debugAlert(msg); //use an alert instead
  26. return;
  27. }
  28. //debugWindow.document.open("text/html"); //it's text/html by default
  29. debugWindow.document.write("<html><head><title>Debug: "+document.title+"</title></head><body></body></html>");
  30. //note: <title> only works in Opera
  31. debugWindow.document.close();
  32. }
  33. debugWindow.document.title = "Debug: "+document.title; //doesn't work in Opera
  34. debugWindow.document.body.innerHTML += msg+"<br />";
  35. }
  36. //write some code to the debug window
  37. function writeCode(code)
  38. {
  39. code = code.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
  40. write('<pre style="background-color:#EEEEEE; margin:1em 0; min-height:2.5em; overflow:auto;">'+code+'</pre>');
  41. }
  42.  
  43. //log a message to the javascript/error console, if supported
  44. function logToConsole(msg)
  45. {
  46. if(window.console) window.console.log(msg);
  47. }
  48.  
  49. //open a new window with specified content (employs maxPopups)
  50. function popup(content, mimeType)
  51. {
  52. if(!continuePopups) return;
  53. if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
  54. {
  55. //This is a safety mechanism in case popups/alerts are displayed in large quantities
  56. // (e.g., if it was called inside a loop)
  57. continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
  58. if(!continuePopups) return;
  59. }
  60. var popWindow = window.open("", ""); //create a new window
  61. if(!popWindow) //a popup blocker caught it
  62. {
  63. debugAlert(content); //use an alert instead
  64. return;
  65. }
  66. if(mimeType) popWindow.document.open(mimeType); //it's text/html by default
  67. //As far as I know, text/xml isn't supported. In Firefox it becomes text/plain instead of text/html.
  68. //Also, setting mimeType to text/plain doesn't always work; use popupCode function below instead
  69. popWindow.document.write(content);
  70. popWindow.document.close();
  71. popWindow.document.title = "Debug Popup: "+document.title;
  72. popupCount++;
  73. }
  74. //open a new window with specified content as code (employs maxPopups)
  75. function popupCode(content)
  76. {
  77. content = content.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
  78. popup("<pre>"+content+"</pre>");
  79. }
  80.  
  81. //displays an alert (employs maxPopups)
  82. function debugAlert(txt)
  83. {
  84. if(!continuePopups) return;
  85. if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
  86. {
  87. //This is a safety mechanism in case popups/alerts are displayed in large quantities
  88. // (e.g., if it was called inside a loop)
  89. continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
  90. if(!continuePopups) return;
  91. }
  92. window.alert(txt);
  93. popupCount++;
  94. }
  95.  
  96. //from http://code.google.com/p/trimpath/wiki/TrimBreakpoint
  97. //allows you to evaluate expressions in the current scope
  98. //usage: Debug.breakpoint(function(s){return eval(s);}, "Note about breakpoint", "1+2");
  99. //the first argument is required as shown (to access the closure)
  100. function breakpoint(evalFunc, msg, initialExpr)
  101. {
  102. msg = msg || "";
  103. var result = "";
  104. var expr = initialExpr || "";
  105. if(expr)
  106. {
  107. try{
  108. result = evalFunc(expr);
  109. }catch(e){
  110. result = e;
  111. }
  112. }
  113. while(true)
  114. {
  115. expr = prompt("BREAKPOINT: " + msg + "\nEnter an expression to evaluate, or Cancel to continue.\n\nResult:\n" +
  116. result, expr);
  117. if(!expr) return; //cancelled or left blank
  118. try{
  119. result = evalFunc(expr);
  120. }catch(e){
  121. result = e;
  122. }
  123. }
  124. }
  125.  
  126. function resetPopups()
  127. {
  128. popupCount = 0;
  129. continuePopups = true;
  130. maxPopups = window.config.Debug.maxPopups; //in case it changed
  131. }
  132.  
  133. //***** public properties and methods *****//
  134.  
  135. return {
  136. write: write,
  137. writeCode: writeCode,
  138. log: logToConsole,
  139. popup: popup,
  140. popupCode: popupCode,
  141. alert: debugAlert,
  142. breakpoint: breakpoint,
  143. resetPopups: resetPopups
  144. }
  145. }(); //initialize Debug

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wizard04 on September 24, 2008

Updated.

You need to login to post a comment.