When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : The Nature of Things


Is there a better way to show debug values from my ASP pages than to Response.Write them in the middle of my page or use a JavaScript alert to pop them up?

[Print this FAQ]

Answer: Whether you are using a sophisticated debugging environment such as Microsoft's Visual Interdev or simply Notepad, the following cut-and-paste script provides an easy way to show output values or whatever kind of information you want without interjecting them into your pages' output or with annoying JavaScript alert boxes.

The server-side JScript object (large code block at bottom) will write client-side JavaScript to your page which then displays any values you want in a single separate broswer window. It has one method: alert(String) which takes any expression that evaluates to a string as a parameter. It also has one property, focus, which determines whether the debug window will get the focus when values are written to it or not.

Directly below is an example of how to use it in your pages, it assumes that you cut and paste the code into a file called Debug.asp - otherwise the usage is exactly as shown.

<!-- #include file="Debug.asp" -->
    Dim msg, intValue
    Set msg = DebugMsg()

    msg.focus = true
    intValue = 4

    msg.alert "Hello"
    msg.alert "The value is " & intValue

The above produces a separate browser window 250 pixels wide and 450 high with the date/time and all the values as shown on two separate lines. Every time the page is refreshed, another line is added to the window, with the date/time and the new values. If you close this child window, it simply re-appears next time the alert method is called.

Important Note: You shouldn't call the alert method at a time in your ASP script when you are already inside a client-side <script></script> block. If you do, you will get a client-side script error and none of your client scripting will work. This script is a tool which writes client code to accomplish its task and it can used improperly, like any tool. (Also, I apologize that it is messy looking code, keep in mind that it's JScript that writes JavaScript that then writes HTML, so it's not likely to look clean, but all that matters is that it works!).

<script language="JavaScript" runat="server">
function mAlert(varValueToShow) {
var re = new RegExp;

re.compile('\\\\', 'gi');
varValueToShow = varValueToShow.replace(re, '\\\\');

re.compile('\'', 'gi');
varValueToShow = varValueToShow.replace(re, '\\\'');

if (!this.initialized) {
var d = new Date();
Response.Write('<sc' + 'ript language="JavaScript">\n');
Response.Write('function debugWin(messageTxt)\n');
Response.Write(' {\n');
Response.Write(' if (!window.debugWindow)\n');
Response.Write(' {\n');
Response.Write(' debugWindow = window.open(\'\', \'windowName\', \'resizable=yes,scrollbars=yes,status=no,location=no,address=no,left=0,top=200,width=250,height=450\');\n');
Response.Write(' debugWindow.document.write(\'<title>Debug window</title>\\n\');\n');
Response.Write(' debugWindow.document.write(\'<hr noshade size=1><font style="font-family: Arial; font-weight: normal; font-size: 8pt;">(' + d.toLocaleString() + ')</font><br>\\n\');\n');
Response.Write(' }\n');
Response.Write(' debugWindow.document.write(\'<font style="font-weight: normal; font-family: Arial;font-size: 8pt; color: #000088; text-align: justify">\' + messageTxt + \'</font>\\n\');\n');
if (this.focus) {
Response.Write(' debugWindow.focus();\n');
Response.Write(' }\n');
Response.Write('</scr' + 'ipt>');
this.initialized = true;
Response.Write('<scr' + 'ipt language="JavaScript">debugWin(\'' + varValueToShow + '<br>\')</scr' + 'ipt>');

function cDebugMsg() {
this.initialized = false;
this.alert = mAlert;
this.focus = true;

function DebugMsg() {
var o = new cDebugMsg;

FAQ posted by Richard Lowe at 10/31/2000 2:44:32 AM to the The Nature of Things category. This FAQ has been viewed 47,057 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article