By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,867 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,867 IT Pros & Developers. It's quick & easy.

How to Translate this Code from VBScript to JavaScript?

nicebasic
P: 91
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:

Expand|Select|Wrap|Line Numbers
  1. <%
  2.   Response.Write "<SCRIPT LANGUAGE=vbscript Event=OnClick For=btnConf>" & vbCrLf _
  3.   & "If f1.txtName.value = " & """""" & "Then" & vbCrLf _
  4.   & "MsgBox " & """Empty Field for the Name!""" & ",vbExclamation," & """Error""" & vbCrLf _
  5.   & "f1.txtName.select" & vbCrLf _
  6.   & "ElseIf f1.txtMail.value = " & """""" & "Then" & vbCrLf _
  7.   & "MsgBox " & """Please Enter Your E-mail!""" & ",vbExclamation," & """Error""" & vbCrLf _
  8.   & "f1.txtMail.select" & vbCrLf _
  9.   & "ElseIf f1.txtPhone.value = " & """""" & "Or Not IsNumeric(f1.txtPhone.value) Then" & vbCrLf _
  10.   & "MsgBox " & """Please Enter Your Phone Number!""" & ",vbExclamation," & """Error""" & vbCrLf _
  11.   & "f1.txtPhone.select" & vbCrLf _
  12.   & "Else" & vbCrLf _
  13.   & "f1.action = " & """SaveOrder.asp?" & Request.QueryString & """" & vbCrLf _
  14.   & "f1.submit" & vbCrLf _
  15.   & "End If" & vbCrLf _
  16.   & "</SCRIPT>"
  17.   If Session("NotComplete") <> "" Then
  18.         Response.Write "<SCRIPT LANGUAGE=vbscript Event=OnLoad For=Window>" & vbCrLf _
  19.         & "MsgBox " & """" & Session("NotComplete") & """" & ",vbExclamation," & """Error""" & vbCrLf _
  20.         & "</SCRIPT>"
  21.   End If
  22. %>
Sep 28 '11 #1

✓ answered by Frinavale

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:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3.   var txtName = document.getElementById('txtName');
  4.   var txtMail = document.getElementById("txtMail');
  5.   var txtPhone = document.getElementById('txtPhone');
  6.   var f1 = document.getElementById('f1');
  7.  
  8.   if(txtName.value == '')
  9.   {
  10.    alert('Empty Field for the Name!')
  11.   }
  12.   else if(txtMail.value== '' )
  13.   {
  14.     alert('Please Enter Your E-mail!');
  15.   }
  16.   else if(txtPhone.value == '' || isNaN(txtPhone.value) == false)
  17.   {
  18.     alert('Please Enter Your Phone Number!');
  19.   }
  20.   else
  21.   {
  22.     f1.action = 'SaveOrder.asp?T2=1&T3=2&T4=3';
  23.   }
  24.  
  25. f1.submit();
  26.  
  27. </script>

Share this Question
Share on Google+
26 Replies


Rabbit
Expert Mod 10K+
P: 12,370
It would help to see just the VBScript code.
Sep 28 '11 #2

nicebasic
P: 91
Unfortunately, I'm not familiar with JavaScript. That's why I have asked experts' help to solve this problem.
Sep 30 '11 #3

Rabbit
Expert Mod 10K+
P: 12,370
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.
Sep 30 '11 #4

jhardman
Expert 2.5K+
P: 3,405
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
Sep 30 '11 #5

nicebasic
P: 91
This is the code as it looks on the browser:

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE=vbscript Event=OnClick For=btnConf>
  2. If f1.txtName.value = ""Then
  3. MsgBox "Empty Field for the Name!",vbExclamation,"Error"
  4. f1.txtName.select
  5. ElseIf f1.txtMail.value = ""Then
  6. MsgBox "Please Enter Your E-mail!",vbExclamation,"Error"
  7. f1.txtMail.select
  8. ElseIf f1.txtPhone.value = ""Or Not IsNumeric(f1.txtPhone.value) Then
  9. MsgBox "Please Enter Your Phone Number!",vbExclamation,"Error"
  10. f1.txtPhone.select
  11. Else
  12. f1.action = "SaveOrder.asp?T2=1&T3=2&T4=3"
  13. f1.submit
  14. End If
  15. </SCRIPT>

Thank you for your attention.
Sep 30 '11 #6

Rabbit
Expert Mod 10K+
P: 12,370
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.getElementById method.
Oct 6 '11 #7

Frinavale
Expert Mod 5K+
P: 9,731
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:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3.   var txtName = document.getElementById('txtName');
  4.   var txtMail = document.getElementById("txtMail');
  5.   var txtPhone = document.getElementById('txtPhone');
  6.   var f1 = document.getElementById('f1');
  7.  
  8.   if(txtName.value == '')
  9.   {
  10.    alert('Empty Field for the Name!')
  11.   }
  12.   else if(txtMail.value== '' )
  13.   {
  14.     alert('Please Enter Your E-mail!');
  15.   }
  16.   else if(txtPhone.value == '' || isNaN(txtPhone.value) == false)
  17.   {
  18.     alert('Please Enter Your Phone Number!');
  19.   }
  20.   else
  21.   {
  22.     f1.action = 'SaveOrder.asp?T2=1&T3=2&T4=3';
  23.   }
  24.  
  25. f1.submit();
  26.  
  27. </script>
Oct 7 '11 #8

Rabbit
Expert Mod 10K+
P: 12,370
@Frinny: VBScript will work in IE and IE only.
Oct 7 '11 #9

jhardman
Expert 2.5K+
P: 3,405
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
Oct 8 '11 #10

nicebasic
P: 91
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.
Oct 11 '11 #11

Frinavale
Expert Mod 5K+
P: 9,731
Let me know how it goes :)

-Frinny
Oct 12 '11 #12

nicebasic
P: 91
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:

Expand|Select|Wrap|Line Numbers
  1.   Response.Write "<script type='text/javascript'>" & vbCrLf _
  2.   & "var txtName = document.getElementById('txtName');" & vbCrLf _
  3.   & "var txtMail = document.getElementById('txtMail');" & vbCrLf _
  4.   & "var txtPhone = document.getElmentById('txtPhone');" & vbCrLf _
  5.   & "var f1 = document.getElementById('f1');" & vbCrLf _
  6.   & "if(txtName.value = '')" & vbCrLf _
  7.   & "{" & vbCrLf _
  8.   & "alert('Empty Field for the Name')" & vbCrLf _
  9.   & "}" & vbCrLf _
  10.   & "else if(txtMail .value = '' )" & vbCrLf _
  11.   & "{" & vbCrLf _
  12.   & "alert('Please Enter Your E-mail!');" & vbCrLf _
  13.   & "}" & vbCrLf _
  14.   & "else if(txtPhone.value = '' || isNaN(txtPhone.value) = false)" & vbCrLf _
  15.   & "{" & vbCrLf _
  16.   & "alert('Please Enter Your Phone Number!');" & vbCrLf _
  17.   & "}" & vbCrLf _
  18.   & "else" & vbCrLf _
  19.   & "{" & vbCrLf _
  20.   & "f1.action = " & "'SaveOrder.asp?" & Request.QueryString & "';" & vbCrLf _
  21.   & "}" & vbCrLf _
  22.   & "f1.submit();" & vbCrLf _
  23.   & "</script>"

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

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. var txtName = document.getElementById('txtName');
  3. var txtMail = document.getElementById('txtMail');
  4. var txtPhone = document.getElmentById('txtPhone');
  5. var f1 = document.getElementById('f1');
  6. if(txtName.value = '')
  7. {
  8. alert('Empty Field for the Name')
  9. }
  10. else if(txtMail .value = '' )
  11. {
  12. alert('Please Enter Your E-mail!');
  13. }
  14. else if(txtPhone.value = '' || isNaN(txtPhone.value) = false)
  15. {
  16. alert('Please Enter Your Phone Number!');
  17. }
  18. else
  19. {
  20. f1.action = 'SaveOrder.asp?T1=1&T2=1';
  21. }
  22. f1.submit();
  23. </script>
  24.  
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.
Apr 9 '12 #13

Frinavale
Expert Mod 5K+
P: 9,731
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
Apr 9 '12 #14

Rabbit
Expert Mod 10K+
P: 12,370
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.
Apr 9 '12 #15

nicebasic
P: 91
It works with no problem in IE.

The problem is with FireFox. I changed the codes from VBScript to JavaScript using the help of Experts in this forum.

It seems something is wrong with quotations or something like that.

If you leave the boxes empty and press Submit, no Alerts will be displayed.

If you fill up the boxes and press Submit, nothing will happen.

I'm really confused.
Apr 9 '12 #16

Rabbit
Expert Mod 10K+
P: 12,370
Like I said in my previous post, it's 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
Apr 9 '12 #17

Frinavale
Expert Mod 5K+
P: 9,731
Rabbit is right!
That JavaScript code is not within a function so it is going to be executed when the page loads. Put it into a function and call the function during the button click event.

-Frinny
Apr 9 '12 #18

nicebasic
P: 91
The original code that used VBScript was like this:

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE=vbscript Event=OnClick For=btnConf>
  2. .......
  3. </SCRIPT>
Can we change the code to this?

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE=JavaScript Event=OnClick For=btnConf>
  2. .......
  3. </SCRIPT>
Is this a valid change?
Apr 9 '12 #19

nicebasic
P: 91
Fortunately, using your great advice, I added a new JavaScript Function like this:
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. function cancel(){
  3. window.location = './cancel.asp'
  4. }
  5. </script>
When you click on the Cancel Button, the above JavaScript will redirect you to the Cancel page and the variables of the current page will be lost.

But:

I have made some changes to the code as you mentioned. Your instructions were really great. I put the code in a JavaScript function like this:

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function decide(){
  3. ..........
  4. }
  5. </script>
  6.  

but I get this error message in FireFox Error Console:


invalid assignment left-hand side
Expand|Select|Wrap|Line Numbers
  1. else if(txtPhone.value = '' || isNaN(txtPhone.value) = false) 
Apr 10 '12 #20

Rabbit
Expert Mod 10K+
P: 12,370
If you're trying to compare values, you use ==. If you're trying to assign a value, you use =.
Apr 10 '12 #21

Frinavale
Expert Mod 5K+
P: 9,731
Ooops, sorry that's my fault. I remember translating the vbScript into JavaScript quickly and not testing anything. I edited/fixed my original post so that it uses the proper comparison operators in the if statements instead of trying to assign things.

-Frinny
Apr 10 '12 #22

nicebasic
P: 91
I edited this line from:
Expand|Select|Wrap|Line Numbers
  1. else if(txtPhone.value = '' || isNaN(txtPhone.value) = false) 
to
Expand|Select|Wrap|Line Numbers
  1. else if(txtPhone.value = '') 
and the above error disappeared.

But another error shows up:

document.getElmentById is not a function

Can't we replace document.getElmentById by something else?
Apr 10 '12 #23

Frinavale
Expert Mod 5K+
P: 9,731
You should have changed the single "=" to a double "==" in the if statements...

Like this:
Expand|Select|Wrap|Line Numbers
  1. else if(txtPhone.value == '' || isNaN(txtPhone.value) == false) 

-Frinny
Apr 10 '12 #24

nicebasic
P: 91
I found a problem in this line:
Expand|Select|Wrap|Line Numbers
  1. var txtPhone = document.getElmentById('txtPhone');
This very simple problem prevented the code from running. I changed it to:
Expand|Select|Wrap|Line Numbers
  1. var txtPhone = document.getElementById('txtPhone');

It was a misspelling. Changing "getElmentById" to "getElementById" fixed it all.

I'm checking the other parts now.

I'm really grateful to Frinavale and Rabbit for your really useful support.
Apr 10 '12 #25

Rabbit
Expert Mod 10K+
P: 12,370
Did you put in those ==? Because you may not get an error but the results would be wrong if you didn't.
Apr 10 '12 #26

nicebasic
P: 91
Yes, Rabbit. I used your great help and great support. Without your help, it wouldn't be possible to solve this complicated problem.

Since this line causes a problem:
Expand|Select|Wrap|Line Numbers
  1. else if(txtPhone.value == '' || isNaN(txtPhone.value) == false) 
I removed the second part of the condition and changed it to:
Expand|Select|Wrap|Line Numbers
  1. else if(txtPhone.value == '') 
I don't know why it doesn't accept any numbers. I enter a number, but it gives me an Alert that I have to enter a valid number. I didn't know how to fix it, so I removed it.

Thank you again, Rabbit.

You contributed so much. You referred to a key point here:

it's 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
Apr 10 '12 #27

Post your reply

Sign in to post your reply or Sign up for a free account.