473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to Translate this Code from VBScript to JavaScript?

nicebasic
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>

26 5406
Rabbit
12,516 Expert Mod 8TB
It would help to see just the VBScript code.
Sep 28 '11 #2
Unfortunately, I'm not familiar with JavaScript. That's why I have asked experts' help to solve this problem.
Sep 30 '11 #3
Rabbit
12,516 Expert Mod 8TB
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
3,406 Expert 2GB
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
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
12,516 Expert Mod 8TB
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
9,735 Expert Mod 8TB
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
12,516 Expert Mod 8TB
@Frinny: VBScript will work in IE and IE only.
Oct 7 '11 #9
jhardman
3,406 Expert 2GB
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
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
9,735 Expert Mod 8TB
Let me know how it goes :)

-Frinny
Oct 12 '11 #12
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
9,735 Expert Mod 8TB
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
12,516 Expert Mod 8TB
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
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
12,516 Expert Mod 8TB
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
9,735 Expert Mod 8TB
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
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
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
12,516 Expert Mod 8TB
If you're trying to compare values, you use ==. If you're trying to assign a value, you use =.
Apr 10 '12 #21
Frinavale
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
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
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
12,516 Expert Mod 8TB
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
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

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

Similar topics

1
by: Martin | last post by:
Shown below is the body of a web page I'm developing. When it loads, a socket connection is established back to a socket that's on my server. I can then send a message from the server and it will...
4
by: Sergio del Amo | last post by:
i, I have the next html page <html> <head> <script> <!-- function insertcode() { var code ="<p> blablabal babala babababab</p><h1>here comes header</h1><span>fadfafa<a...
3
by: fred4534 | last post by:
Hello, I'm using the GoAhead embedded Webserver (http://webserver.goahead.com/webserver/webserver.htm) which can use only ASP and a set of javascript instructions. I'm trying to make an upload...
4
by: Carlo Marchesoni | last post by:
I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes=".....";) Now I need to run some code behind code BEFORE this JavaScript...
10
by: Shadow Lynx | last post by:
That subject packs a whallop, so let me explain in better detail what's happening and how it relates to ASPX pages... In a nutshell, if the first <script /on a page is of type "text/vbscript",...
15
by: mistral | last post by:
I want find code for clickable thumbnails, when click on small picture, a big image will popup in new window, sized to fit picture. Same as standard javascript image previewer, but without using...
7
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I want to learn C# in the near future. But for now, I would be more than happy if someone could translate this short c# source code into c++. (I searched the web for c++ equivalents but after...
2
by: cromoglic | last post by:
Hello. I am new to javascript, and need help with something:) I came across this "hack my site"-site, and cant get past level 4 (lol);P I've got the source code: <script src="JavaScript"...
10
by: urib | last post by:
Suppose that I have some simple function in C like the following when I want to use it in javascript code int plus1(int a) { return a+1; } I am using visual C++2008 for my C code. I...
1
by: najmi | last post by:
hai.. i have used jquery fullcalendar for my application and i have one problem that is how to display the event from the database.i have found the sample given in php but i want it in jsp.can...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.