Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

javascript - How can I write a "debug msg" to the console or elsewise in an old web site?

I'm adding a bit of functionality to an old ("old" as in "olde"; as in Rip-van-Winkle olde) website that uses VBScript.

The upshot of my problem is that certain logic seems to always equate to a single result (IsNewBusiness is always apparently assigned a value of FALSE), whereas it should sometimes equate to the opposite, that logic being:

IsNewBusiness = TRUE 'default (if record not found)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
End If

So, since the logic is always equating to false, I have to assume that this line:

IsNewBusiness = adoRS.Fields.Item(0).Value <> 0

...is being reached, and that adoRS.Fields.Item(0).Value is always 0

As you can see in the above code, the IsNewBusiness Boolean is set to TRUE to start with, but never remains true, although it should be (reset) to TRUE on some occasions (the query result should, given certain parameters, return a value of "-1" rather than 0).

This question is related to but really separate from a question I asked here:

So, to get to the crux of the biscuit, I want to make absolutely certain that the if block shown above is being reached, by writing out a "debug msg" to the console or something similar.

Based on legacy code in this site, I made this attempt to partner with javascript to those ends:

ReturnMsg = "Made it to IsNewBusiness logic"
Response.Write("<script type=""text/javascript"">" & vbCrLf)
Response.Write("alert ('" & ReturnMsg & "');" & vbCrLf)
Response.Write("</script>" & vbCrLf)

...but it doesn't seem to work - I never see that msg.

I also tried this:

Response.Write("window.alert(ReturnMsg);")

...and this:

Response.Write("console.log(ReturnMsg);")

...with no difference in results. All that I see in my console is:

enter image description here

How can I accomplish an "alert" or a console log msg, or some such, using either VBScript, or javascript, or whatever?

UPDATE

I even tried some inline javascript, like so:

adoRS.Close()

<script language="javascript">
window.alert("Made it past the IsNewBusiness logic");
</script>

...but it blew up...

UPDATE 2

I tried the trick mentioned by Kul-Tigin, but it didn't seem to work for me.

I changed this code:

currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 'Is this ever reached?
End If
adoRS.Close()

...to this:

currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
ReturnMsg = "Before the If Not adoRS.EOF block"
                Response.Write("<script type=""text/javascript"">" & vbCrLf)
                Response.Write("<!--" & vbCrLf)
                Response.Write("alert ('" & ReturnMsg & "');" & vbCrLf)
                Response.Write("-->" & vbCrLf)
                Response.Write("</script>" & vbCrLf)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 'Is this ever reached?
    ReturnMsg = "Entered the If Not adoRS.EOF block"
                Response.Write("<script type=""text/javascript"">" & vbCrLf)
                Response.Write("<!--" & vbCrLf)
                Response.Write("alert ('" & ReturnMsg & "');" & vbCrLf)
                Response.Write("-->" & vbCrLf)
                Response.Write("</script>" & vbCrLf)
End If
adoRS.Close()

...ran the site to get to that page, selected View Source, searched for "the If Not adoRS.EOF block", and there were no matches. Am I doing it wrong?

UPDATE 3

I also tried this (the above was based on existing code in the page, the below is based on the commenter's snippet):

Response.Write("<!-- Before the If Not adoRS.EOF block -->")
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 'Is this ever reached?
    Response.Write("<!-- After the If Not adoRS.EOF block -->")
End If

UPDATE 4

The only way I was able to get the debug msg I added to show up in the View Source was to insert some javascript that threw an error. Adding this javascript:

<script language="javascript">
    window.alert("Before the If Not adoRS.EOF block js");
</script>

In some context:

Response.Write("<!-- Before the If Not adoRS.EOF block -->")
<script language="javascript">
    window.alert("Before the If Not adoRS.EOF block js");
</script>
If Not adoRS.EOF Then

...caused this to appear on the page:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30636: '>' expected.

Source Error:



Line 129:        IsNewBusiness = TRUE 'default (if record not found)
Line 130:        Response.Write("<!-- Before the If Not adoRS.EOF block -->")
Line 131:        <script language="javascript">
Line 132:            window.alert("Before the If Not adoRS.EOF block js");
Line 133:        </script>


Source File: C:EnhancedMonthlySalesReportingcustomerreportingnetcustomerreportingnetpagescustmaint_entry.aspx    Line: 131 

...and this in the View Source:

Line 129:        IsNewBusiness = TRUE 'default (if record not found)
Line 130:        Response.Write(&quot;&lt;!-- Before the If Not adoRS.EOF block --&gt;&quot;)
<font color=red>Line 131:        &lt;script language=&quot;javascript&quot;&gt;
</font>Line 132:            window.alert(&quot;Before the If Not adoRS.EOF block js&quot;);
Line 133:        &lt;/script&gt;</pre></code>

...so I'm still swimming upstream in Leviathan-infested waters, it seems.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's debug this. One of three things must be the case:

  1. The code is run, and enters the If, and sets IsNewBusiness to true.
  2. The code is run, and enters the If, and sets IsNewBusiness to false.
  3. The code is run, and doesn't enter the If. Therefore, IsNewBusiness must be set to true.
  4. The code is never run in the first place. IsNewBusiness has an unknown value.

A little bit of thinking will convince you that those cases are exhaustive: One of them must be what's happening.

Your job, then, is to figure out which of those four cases is actually happening. Ideally, you could attach to the site with a debugger (have you used Visual Studio's "Attach to Process" option?) and set breakpoints in the code and just see which breakpoints get hit and which don't.

But let's say you can't use a debugger, for whatever reason. So if it were me, I'd annotate the code with Response.Write() or Trace() calls to emit output, as several people suggested in the comments, to indicate as many of the possibilities as possible. Since Response.Write() is a dead giveaway as to whether the code ran (you either see its output or you don't), it's a good choice. Cases 1 and 2 collapse into a single case (right now, you probably only care whether the computer got into the If, not what the code inside did after the computer got there).

So I'd temporarily annotate the code like this:

IsNewBusiness = TRUE 'default (if record not found)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
    Response.Write("<h1>INSIDE IF</h1>")
Else
    Response.Write("<h1>INSIDE ELSE</h1>")
End If

(In short: When debugging, you can safely mutate the content of the page itself in order to answer questions, as long as you clean up your mutations later!)

This then allows you to answer the cases above when you load the page:

  • If you see "INSIDE IF" in big bold text, you know that the computer went inside the If statement, and adoRS.EOF must be false.
  • If you see "INSIDE ELSE" in big bold text, you know the computer didn't go inside the If statement, and adoRS.EOF must be true.
  • If you see neither, the computer never reached this code at all, and your problem is elsewhere.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...