-5

So my boss asked me to look into an issue where they are porting old VBscript scripts into Javascript scripts but where the Javascript has incorrect program flow. The only problem is that I'm really ignorant about both, but if your boss asks you, then you have to make a plan. ;-)

In the VBscript, the user was given a choice and if "No" the next screen/view wasn't shown. However, with the Javascript, the next screen/view is loaded irrespective of whether the user chooses "No/Cancel" or not. This should not happen.

Here is the VBscript, the important part is between the //:

vEffectiveDate  = ScreenForm("smoke.Effective Date").Value
vLastRunDate    = ScreenForm("smoke.Last Run Date").Value
sStatus         = ScreenForm("smoke.Calc Status").Value

vMsg = ""

If Len(sStatus) = 0 Then
    sStatus = "SUCCESFUL"
    ScreenForm("smoke.Calc Status").Value = sStatus
End If

If Len(vEffectiveDate) > 0 Then

    If Not IsDate(vEffectiveDate) Then 
        vMsg = vMsg & "[Effective Date] Is not a date." & Chr(13)
    
    ElseIf cdate(vEffectiveDate) <= cdate(vLastRunDate) Then
        vMsg = vMsg & "[Effective Date] cannot be on/before " & vLastRunDate &"." & Chr(13)
    End IF

End If

//////////////////////////////////////////////////////////////////////////////
If UCASE(sStatus) <> "SUCCESFUL" Then
    sResponse = MsgBox ("Forecast calculation still busy. Results might not be accurate. Continue?", (vbYesNo), "WARNING")
    If sResponse = vbNo Then
        MsgBox cstr("Screen will refresh. Please click on Update to try again."), (vbOKOnly), "INFORMATION"
        ScreenForm("smoke.Calc Status").Value = "REFRESH"
        'msErr = "ABORT"
    End If
End If
//////////////////////////////////////////////////////////////////////////////

If vMsg <> "" Then
MsgBox (vMsg)
msErr = "ERROR"
End If

Here is the Javascript, the important part is between the //:

 var vEffectiveDate="";
var vLastRunDate="";
var sStatus="";
var vMsg="";
var sResponse="";
vEffectiveDate = document.getElementById("Smoke.Effective Date").value;
vLastRunDate = document.getElementById("Smoke.Last Run Date").value;
sStatus  = document.getElementById("Smoke.Calc Status").value;
vMsg = "";
if ((sStatus).length == 0  ){
    sStatus = "SUCCESFUL";
    document.getElementById("Smoke.Calc Status").value= sStatus;
}
if ((vEffectiveDate).length > 0  ){
    if (!isDate(vEffectiveDate)  ){
        vMsg = vMsg+"[Effective Date] Is not a date." + ";\r\n";
    } else if (  moment( toDate(vEffectiveDate)).isBefore(toDate(vLastRunDate)) ){
        vMsg = vMsg+"[Effective Date] cannot be on/before "+vLastRunDate+"." + ";\r\n";
    }
}
///////////////////////////////////////////////////////////
if ((sStatus).toUpperCase() != "SUCCESFUL"  ){
 $.confirm({title:  "Confirmation",columnClass: 'col-md-6 col-md-offset-3', content:"Forecast calculation still busy. Results might not be accurate. Continue?",
buttons: {confirm: function() { sResponse= 1;},
cancel: function() {sResponse= 2;return;}}});
if (sResponse == 2  ){
        $.alert({title:  "INFORMATION",columnClass: 'col-md-6 col-md-offset-3', content:("Screen will refresh. Please click on Update to try again.").toString(),});
        document.getElementById("Smoke.Calc Status").value= "REFRESH";
  msErr = "ABORT";
    }   
}
//////////////////////////////////////////////////////////


if (vMsg != ""  ){
$.alert({title: 'Validation Message',columnClass: 'col-md-6 col-md-offset-3', content:(vMsg),});
msErr = "ERROR";
}

So, I've seen that there is an async-await-promises concept in Javascript, but it seems that it is not supported by Internet Explorer, and we need to keep in mind that some of our users actually still use IE...sigh... So it seems that I won't be able to use an async-await concept, not that I'm sure that it would be necessary in this situation.

Then, I also read here about a submit button for a form, and that that might be my problem too?

I'm so clueless about Javascript, I don't even know how to change the button's default behaviour if that is the problem.

I would appreciate someone helping me in the right direction so that I can learn and grow.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Igavshne
  • 699
  • 7
  • 33
  • `$` is provided by a library? What is it? – trincot Oct 08 '20 at 19:41
  • Which are the ` – trincot Oct 08 '20 at 19:43
  • Seems the issue lies with the `$.confirm()` implementation, does that come from a library or framework? Is it this - [jquery-confirm](https://craftpip.github.io/jquery-confirm/)? – user692942 Oct 08 '20 at 19:50
  • 1
    What have to understand is that `MsgBox()` and `$.confirm()` are not equivalent as `MsgBox()` is a built-in VBScript function that produces a GUI where as `$.confirm()` is just wrapper for a HTML DIV that behaves like a popup but will not block until complete. To make it do that you'll need to move the logic around the response code into the callback instead of just returning `sResponse = 2`. – user692942 Oct 08 '20 at 20:01
  • @Lankymart, yes it comes from that jquery-confirm link. My boss had added that on the issue documentation. She was doing the porting from VBscript, so I wasn't involved initially. – Igavshne Oct 09 '20 at 10:31
  • @trincot, it comes from [jquery-confirm](https://craftpip.github.io/jquery-confirm/) as was mentioned in the other comments. – Igavshne Oct 09 '20 at 10:34
  • 1
    Does this answer your question? [Overriding Javascript Confirm() while preserving the callback](https://stackoverflow.com/a/3886916) – user692942 Oct 12 '20 at 07:43
  • @Lankymart, Perhaps. I'll have to try to decipher, implement and test it. For someone knowledgeable with Javascript it seems it would be very easy and they would be able to put it into code just by understanding the general gist. Anyways, I'll try. I was also wondering whether something other than the confirm would be better suited to replace the VBscript's MsgBox? – Igavshne Oct 12 '20 at 11:09
  • 1
    @Igavshne The in-built JavaScript `confirm()` function would be a more direct replacement to `MsgBox()` in VBScript - [Window.confirm()](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm) – user692942 Oct 12 '20 at 12:22
  • 1
    Does this answer your question? [Javascript equivalent function for VBScript MsgBox()](https://stackoverflow.com/q/14584942). Like I've said this has all been answered before. – user692942 Oct 12 '20 at 12:25
  • 1
    Editing Questions to improve them (e.g. clarification, adding additional information, etc.) *is encouraged*. However, editing a Question to change it into a different question, which invalidates one or more answers, is against policy on Stack Overflow. Your edit here did so. The policy is that other users with edit privileges should proactively revert such changes. I have reverted your edit. You *are encouraged to [ask a new Question](/questions/ask)*, perhaps with a link to this one for additional context. We want to help, but your new/additional issue needs to be a new Question. – Makyen Oct 12 '20 at 18:09
  • If you want the Markdown source for your question prior to it being rolled back (e.g. in order to ask a new question), you can find it [here](https://stackoverflow.com/revisions/5db8f1c8-e1a6-44b2-873d-14cf4fee3380/view-source). – Makyen Oct 12 '20 at 18:18
  • @Makyen, I provided the update, because it is how my thoughts flowed naturally. Now I have the choice to either delete my question (there goes the additional context for the new question) or to edit this question, but if I edit this question, someone comes along and bashes me over the head for thinking in wider terms to what I understood my problem was in the first place. My updates did NOT invalidate the answer. It actually enhances it by stating clearly that I have implemented the answer and that I have not yet found a suitable solution. – Igavshne Oct 13 '20 at 11:19
  • @Makyen, the way you have moderated the question, the given answer seems to solve it, and it seems to be a duplicate of another question. However, that is not the _reality_. – Igavshne Oct 13 '20 at 11:37
  • You are welcome to ask a new question. The question you asked here was answered. As the question existed at the time it was answered, it appears to be a duplicate to me. The edit which you made to the question changed it such that the answer was diminished from being a complete answer to being a partial answer, because the answer didn't account for the additional information (found from the answer). That's not fair to the user who put in effort to answer. There's nothing wrong with needing to ask a new question to account for implementing new code. – Makyen Oct 13 '20 at 18:18
  • While your new situation is still to resolve the overall problem you are having (from a business/get my program working POV), it's a different question from Stack Overflow's POV. Stack Overflow *isn't* a site where you ask a question like "I need to get this larger program working." It's where you go for a solution to *very specific* problems (how to do some very limited thing; fix a single error/bug (or very closely related errors); etc.), which may just be one of many that need to be resolved for your overall solution. It can easily be that the answer to one question leads to more questions. – Makyen Oct 13 '20 at 18:20
  • @Makyen, I am really upset at the way you have handled this. Especially since I mentioned that I am ignorant at both VBscript and Javascript. I work as a contractor for this employer for only a couple of hours per day. I haven't even had a chance to work through all the linked questions to see whether I really have a duplicate, whether this or that answers my question or whatever. I'm so clueless, I don't even properly know what to ask, but hey, enforcing policy is more important than sticking around to help the struggling user. – Igavshne Oct 13 '20 at 19:50

1 Answers1

1

Indeed, the second part of the code (with the alert) is executed irrespective of the outcome of the confirm prompt.

The main difference with $.confirm and VBA's MsgBox, is that MsgBox is blocking the further execution of code, while $.confirm does not do that. So that means that the code that follows it (the if (sResponse == 2) part), is executed immediately. At that time no change was made to sResponse (as no button was clicked), so that comes too early.

However, $.confirm accepts some functions as arguments, which will be called when a button is pressed on the popup. Actually, your code already passes such functions, but they don't do anything else than setting sResult. So you should move the second part of the code (with the alert) in such a function.

First let's format your code a bit so it becomes more readable, and you can better spot those callback functions:

if ((sStatus).toUpperCase() != "SUCCESFUL") {
    $.confirm({
        title:  "Confirmation",
        columnClass: 'col-md-6 col-md-offset-3', 
        content:"Forecast calculation still busy. Results might not be accurate. Continue?",
        buttons: {
            confirm: function() {
                // This executes when button is clicked
                sResponse= 1;
            },
            cancel: function() {
                // This executes when button is clicked
                sResponse= 2;
                return;
            }
        }
    });
    // This executes immediately (without waiting for button click)
    if (sResponse == 2  ){
        $.alert({
            title: "INFORMATION",
            columnClass: 'col-md-6 col-md-offset-3',
            content:("Screen will refresh. Please click on Update to try again.").toString(),
        });
        document.getElementById("Smoke.Calc Status").value= "REFRESH";
        msErr = "ABORT";
    }   
}
// Also this executes too early when confirm was executed:
if (vMsg != ""){
    $.alert({
        title: 'Validation Message',
        columnClass: 'col-md-6 col-md-offset-3',    
        content:(vMsg),
    });
    msErr = "ERROR";
}

I added comments to show where the important parts of the code are.

Now move that second part inside the callback function that relates to response #2:

if (sStatus.toUpperCase() != "SUCCESFUL") {
    $.confirm({
        title:  "Confirmation",
        columnClass: 'col-md-6 col-md-offset-3', 
        content:"Forecast calculation still busy. Results might not be accurate. Continue?",
        buttons: {
            confirm: function() {
                sResponse= 1;
                processMessage(); // <--- added this
            },
            cancel: function() {
                sResponse= 2;
                // Moved code here, as it needs to execute when Cancel is clicked
                $.alert({
                    title: "INFORMATION",
                    columnClass: 'col-md-6 col-md-offset-3',
                    content: "Screen will refresh. Please click on Update to try again.",
                    // Code that should execute when alert is closed:
                    onAction: function () {
                        document.getElementById("Smoke.Calc Status").value= "REFRESH";
                        msErr = "ABORT";
                        processMessage(); // <--- added this
                    }
                });
            },
        }
    });
} else { // <-- added
    processMessage();
}

function processMessage() {
    // Moved code in a function, as it should only execute after confirm/alert is closed 
    if (vMsg != "") {
        $.alert({
            title: 'Validation Message',
            columnClass: 'col-md-6 col-md-offset-3',    
            content: vMsg,
        });
        msErr = "ERROR";
    }
}

I didn't test this code, as it has dependencies I do not know of.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you this has been very helpful, particularly because I am a bit lost. Yes I understand that you can't test the code. I'll have a look and let you know. – Igavshne Oct 09 '20 at 14:34
  • please see my Update. – Igavshne Oct 10 '20 at 12:35
  • I cannot help you more I am afraid. The behaviour related to 5 seconds must be related to other code that is not in your question. I cannot reproduce your problem. You should debug, using the console, setting breakpoints, ...etc, to pinpoint what is happening. – trincot Oct 10 '20 at 12:40
  • Thank you, I appreciate your time and help. :-) – Igavshne Oct 12 '20 at 11:45
  • As you added to your question: yes, `MsgBox` maps to `confirm` and `prompt`, which are native in Web-based JavaScript. The latter block execution also like `MsgBox`. – trincot Oct 12 '20 at 11:53
  • 1
    Since it seems there are additional things to take into account, I must first understand the whole situation before I can decide whether this answer is sufficient. I only work a couple of hours per day for this employer, so it might take some time. However, I won't forget your kind and useful answer. – Igavshne Oct 13 '20 at 20:04