How to Translate this Code from VBScript to JavaScript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicebasic
    New Member
    • Aug 2010
    • 91

    #1

    How to Translate this Code from VBScript to JavaScript?

    I have a piece of code in one of my web applications that has been written with ASP Classic.

    Unfortunately, some part of the code is VBScript and does not run in FireFox. I need to change it to JavaScript, but I'm not familiar with JavaScript.

    Can anyone help me?

    This is the piece of code in VBScript. I need to have a JavaScript version of the following code:

    Code:
    <%
      Response.Write "<SCRIPT LANGUAGE=vbscript Event=OnClick For=btnConf>" & vbCrLf _
      & "If f1.txtName.value = " & """""" & "Then" & vbCrLf _
      & "MsgBox " & """Empty Field for the Name!""" & ",vbExclamation," & """Error""" & vbCrLf _
      & "f1.txtName.select" & vbCrLf _
      & "ElseIf f1.txtMail.value = " & """""" & "Then" & vbCrLf _
      & "MsgBox " & """Please Enter Your E-mail!""" & ",vbExclamation," & """Error""" & vbCrLf _
      & "f1.txtMail.select" & vbCrLf _
      & "ElseIf f1.txtPhone.value = " & """""" & "Or Not IsNumeric(f1.txtPhone.value) Then" & vbCrLf _
      & "MsgBox " & """Please Enter Your Phone Number!""" & ",vbExclamation," & """Error""" & vbCrLf _
      & "f1.txtPhone.select" & vbCrLf _
      & "Else" & vbCrLf _
      & "f1.action = " & """SaveOrder.asp?" & Request.QueryString & """" & vbCrLf _
      & "f1.submit" & vbCrLf _
      & "End If" & vbCrLf _
      & "</SCRIPT>"
      If Session("NotComplete") <> "" Then
    		Response.Write "<SCRIPT LANGUAGE=vbscript Event=OnLoad For=Window>" & vbCrLf _
    		& "MsgBox " & """" & Session("NotComplete") & """" & ",vbExclamation," & """Error""" & vbCrLf _
    		& "</SCRIPT>"
      End If
    %>
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It would help to see just the VBScript code.

    Comment

    • nicebasic
      New Member
      • Aug 2010
      • 91

      #3
      Unfortunately, I'm not familiar with JavaScript. That's why I have asked experts' help to solve this problem.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        What you have there is a server side VBScript that outputs client side VBScript. What you want to convert is the client side VBScript because that's what's affected by a browser. Since that's what you want to convert, it would help to see the client side VBScript by itself.

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          This is a really difficult question, especially since you posted the raw ASP code, instead of the interpreted code with the server-side code gone. Instead we look at this and see jumbles of VBScript, some of which is server-side and some is client-side.

          Post the code as it looks on the browser - right-click and "view source". This should have no server-side code left.

          Jared
          Last edited by jhardman; Sep 30 '11, 04:41 AM. Reason: typo

          Comment

          • nicebasic
            New Member
            • Aug 2010
            • 91

            #6
            This is the code as it looks on the browser:

            Code:
            <SCRIPT LANGUAGE=vbscript Event=OnClick For=btnConf>
            If f1.txtName.value = ""Then
            MsgBox "Empty Field for the Name!",vbExclamation,"Error"
            f1.txtName.select
            ElseIf f1.txtMail.value = ""Then
            MsgBox "Please Enter Your E-mail!",vbExclamation,"Error"
            f1.txtMail.select
            ElseIf f1.txtPhone.value = ""Or Not IsNumeric(f1.txtPhone.value) Then
            MsgBox "Please Enter Your Phone Number!",vbExclamation,"Error"
            f1.txtPhone.select
            Else
            f1.action = "SaveOrder.asp?T2=1&T3=2&T4=3"
            f1.submit
            End If
            </SCRIPT>

            Thank you for your attention.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              This is actually fairly simple.

              In javascript, lines of code end in a semicolon so you'll need to put those in.

              There's no MsgBox but you can use alert in its place.

              You'll need to change the if conditionals to the javascript version.

              And I think that's pretty much it in terms of the port. You have have to finagle a bit with the dom. I can't remember if you have direct access to the dom subelements like you do in vbscript so you may have to use the document.getEle mentById method.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                What I don't get is why you are attempting to execute vb script in a web browser??? No wonder it doesn't work in FireFox.

                Here's something quick that I did based on what you've posted... it is NOT guaranteed to work:

                Code:
                <script type="text/javascript">
                
                  var txtName = document.getElementById('txtName');
                  var txtMail = document.getElementById("txtMail');
                  var txtPhone = document.getElementById('txtPhone');
                  var f1 = document.getElementById('f1');
                
                  if(txtName.value == '')
                  {
                   alert('Empty Field for the Name!')
                  }
                  else if(txtMail.value== '' )
                  {
                    alert('Please Enter Your E-mail!');
                  }
                  else if(txtPhone.value == '' || isNaN(txtPhone.value) == false)
                  {
                    alert('Please Enter Your Phone Number!');
                  }
                  else
                  {
                    f1.action = 'SaveOrder.asp?T2=1&T3=2&T4=3';
                  }
                
                f1.submit();
                
                </script>
                Last edited by Frinavale; Apr 10 '12, 07:58 PM.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  @Frinny: VBScript will work in IE and IE only.

                  Comment

                  • jhardman
                    Recognized Expert Specialist
                    • Jan 2007
                    • 3405

                    #10
                    Client-side vbscript is a throw-back from the netscape/IE wars of the 90s. It was never standardized and was never a good idea. Even when I used to have mixed client- and server-side scripting it always made sense to me to switch back and forth so I could say "this part is vbscript, so must be server-side, and this part is javascript so must be client-side" as I was re-reading the code.

                    Really, kudos to you, nicebasic. Responsible web owners need to be removing it wherever they find it.

                    Jared

                    Comment

                    • nicebasic
                      New Member
                      • Aug 2010
                      • 91

                      #11
                      Thank you "Frinavale" for the JavaScript code you have posted. I'll test your code. Unfortunately, my Internet Provider had a problem with its servers. I didn't have any Internet Connection in the past week. That's why I couldn't check this thread. I hope I can solve this problem with your help.

                      I'm not familiar with JavaScript. I'm only familiar with VB 6.0 & ASP Classic & VBScript.

                      The above VBScript does not belong to me. One of my friends has written that code. He doesn't know how to translate it to JavaScript either.

                      Thank you everybody for your kind help on this question.

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Let me know how it goes :)

                        -Frinny

                        Comment

                        • nicebasic
                          New Member
                          • Aug 2010
                          • 91

                          #13
                          I hope that you would excuse me for such a long absence!

                          Dear Frinavale,

                          I hope that you would excuse me for such a long absence. Something happened to my ISP and I lost many things.

                          Moreoever, the company that hosted my files lost all of them and wasn't able to restore them.

                          It was a big disaster. I have heard that hosting companies should provide RAID for data loss.

                          Anyway, I tested your valuable JavaScript code. I had to put your code in some ASP code to be generated on the Server.

                          This is your code wrapped in some ASP code:

                          Code:
                            Response.Write "<script type='text/javascript'>" & vbCrLf _
                            & "var txtName = document.getElementById('txtName');" & vbCrLf _
                            & "var txtMail = document.getElementById('txtMail');" & vbCrLf _
                            & "var txtPhone = document.getElmentById('txtPhone');" & vbCrLf _
                            & "var f1 = document.getElementById('f1');" & vbCrLf _
                            & "if(txtName.value = '')" & vbCrLf _
                            & "{" & vbCrLf _
                            & "alert('Empty Field for the Name')" & vbCrLf _
                            & "}" & vbCrLf _
                            & "else if(txtMail .value = '' )" & vbCrLf _
                            & "{" & vbCrLf _
                            & "alert('Please Enter Your E-mail!');" & vbCrLf _
                            & "}" & vbCrLf _
                            & "else if(txtPhone.value = '' || isNaN(txtPhone.value) = false)" & vbCrLf _
                            & "{" & vbCrLf _
                            & "alert('Please Enter Your Phone Number!');" & vbCrLf _
                            & "}" & vbCrLf _
                            & "else" & vbCrLf _
                            & "{" & vbCrLf _
                            & "f1.action = " & "'SaveOrder.asp?" & Request.QueryString & "';" & vbCrLf _
                            & "}" & vbCrLf _
                            & "f1.submit();" & vbCrLf _
                            & "</script>"

                          And, this is your code as it looks on the browser:

                          Code:
                          <script type='text/javascript'>
                          var txtName = document.getElementById('txtName');
                          var txtMail = document.getElementById('txtMail');
                          var txtPhone = document.getElmentById('txtPhone');
                          var f1 = document.getElementById('f1');
                          if(txtName.value = '')
                          {
                          alert('Empty Field for the Name')
                          }
                          else if(txtMail .value = '' )
                          {
                          alert('Please Enter Your E-mail!');
                          }
                          else if(txtPhone.value = '' || isNaN(txtPhone.value) = false)
                          {
                          alert('Please Enter Your Phone Number!');
                          }
                          else
                          {
                          f1.action = 'SaveOrder.asp?T1=1&T2=1';
                          }
                          f1.submit();
                          </script>
                          When I run this code on the specified page, nothing seems to happen. I press the Submit Button, but nothing happens. Even if you leave the fields EMPTY, no Error Messages or Alert Boxes will be displayed.

                          It seems as if something is wrong. The original code, without any changes, can be run in IE, but due to the lack of VBScript support in FireFox, it does not work in FF. I was trying to translate this code to JavaScript using expert programmers in this great forum.

                          I hope you and great programmers like you can help me solve this problem.

                          Thank you again.

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            There's probably a JavaScript error on the page.
                            Use the JavaScript debugging tools that come with the browser that you prefer to work with. If you are using FireFox, install a plugin called FireBug (it'll help you debug JavaScript and HTML/CSS stuff).

                            Once you have started JavaScript debugging, you should see errors pop up.... Once you have the errors, it will be easier to track down what is going on.

                            My first guess would be that you have not named your HTML elements the same way as you did the first time. You should have HTML <input type="text"/> elements with IDs 'txtName', 'txtMail', and 'txtPhone'. You should also have a submit button with the id 'f1'.

                            If you are missing any of these, or you have the different IDs, then your script will crash and your submit will never be called.

                            -Frinny

                            Comment

                            • Rabbit
                              Recognized Expert MVP
                              • Jan 2007
                              • 12517

                              #15
                              Isn't that going to run as soon as the page loads and before the form elements are available for use? You need to tie it to the event on the button.

                              Comment

                              Working...