468,505 Members | 1,937 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

How can I translate the following lines from ASP VBScript to JavaScript?

nicebasic
I have a small page with ASP. In one part of the program, when the user clicks on a button, I use the following code to call a VBScript Subroutine with the name of SubmitForm:


Expand|Select|Wrap|Line Numbers
  1. <input type="button" language="vbscript" onclick="Submitform(<%=I%>)">

The code that creates the Subroutine SubmitForm is found at the HEAD section of the page. Here's the code that generates SubmitForm Subroutine:


Expand|Select|Wrap|Line Numbers
  1.   Response.Write "<script language='vbscript'>" & vbCrLf _
  2.   & "<!--" & vbCrLf _
  3.   & "Sub Submitform(Number)" & vbCrLf _
  4.   & "frm.action = " & """AddItem.asp?idnum=" & IdNumber & "&pgnum=" & """" & " & Number" &  vbCrLf _
  5.   & "frm.submit" & vbCrLf _
  6.   & "End Sub" & vbCrLf _
  7.   & "-->" & vbCrLf _
  8.   & "</script>"


Since I wish to make this page compatible with FireFox, I need to translate the above line from VBScript to JavaScript. As you know, FireFox does not support VBScript by itself. So, I really need to fix this problem.

Thank you for your help.
Aug 23 '10 #1

✓ answered by jhardman

I just tested this, it worked fine. There were a couple mistakes I had made in the original code I posted, notice the parentheses instead of brackets.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.     <title>Test javascript function</title>
  6.  
  7.  
  8.  
  9.     <script type="text/javascript" language="javascript">
  10.       <!--
  11.         function Submitform() {
  12.  
  13.             document.getElementById("frmMain").action = "requesttest.asp";
  14.             document.getElementById("frmMain").submit();
  15.         }
  16.  
  17.         function changeHeader() {
  18.             alert(document.getElementById("h1").innerHTML);
  19.             document.getElementById("h1").innerHTML = "header is changed";
  20.             //alert(document.getElementById("frmMain").action + "'");
  21.             //document.getElementById("frmMain").action = "requesttest.asp";
  22.             //alert(document.getElementById("frmMain").action + "'");
  23.         }
  24.       -->
  25.     </script>
  26.  
  27.  
  28. </head>
  29. <body onload="changeHeader()">
  30. <h1 id="h1">Here's the main header</h1>
  31. <form method="Post" name="frmMain" id="frmMain" action="">
  32. <img alt="click here to submit" src="jhdotcomLogo.png" onclick="Submitform()"
  33. border="2" height="50" width="50" id="myPic"/>
  34. <input type="text" name="testText" />
  35. </form>
  36. </body>
  37. </html>
  38.  
Jared

14 2378
Jerry Winston
145 Expert 100+
Your "translation" shouldn't be too difficult. However, I caution you not to copy/paste solutions from the web when translating. Alot of coders out there are gurus or frequently use really, really esoteric features of javascript. You need to know only a little javascript to perform your conversion, but I'd suggest learning a little more about JavaScript before you implement a "solution" you cannot support developed by a programmer who's style you don't really know.

JavaScript
Aug 23 '10 #2
jhardman
3,405 Expert 2GB
I agree with Jerry, but I will give you a solution. I would recommend that you take such a long passage out of the asp response.write command, and just write it. Once you do that, translating it to javascript with no extra flurries is very simple.
Expand|Select|Wrap|Line Numbers
  1. %>
  2. <script language="javascript">
  3.   <!--
  4.   function Submitform(Number) {
  5.   document.getElementByID["frmID"].action = "AddItem.asp?idnum=<%=IdNumber%>&pgnum=" + Number;
  6.   document.getElementByID["frmID"].submit();
  7.   }
  8.   -->
  9.   </script>
  10.    <%
barring some odd typos, this should work.

Jared
Aug 24 '10 #3
Thank you for you kind attention, Jerry.

I'll try to do the same as you advised me to. But, for the moment, I need the solution.

I'm planning to take a JavaScript course to learn its basic commands.
Aug 25 '10 #4
Thank you a million, jhardman.

I think your solution could be very near to the final solution.

I tested it on my program, but it didn't work.

I suppose there might be a problem with lines 5 & 6.

In my program, the actual name of the Form being used is "frm" and this might be the cause of the error. Also, I haven't set an ID for my Form. You have used document.getElementByID to set some values for the specified Form, while the Form doesn't have an ID to be used in this case.

I know that my question may sound so easy for you, but as I don't know anything about the syntax of JavaScript, I can't solve this problem. I'm so much busy with ASP and VBScript code that I can't think about other things.

I replaced your code with the code I have given above. But, when I pressed the input button, nothing happened in FireFox. Surprisingly, IE showed "Error on page" in the Status Bar.

I'm really grateful to you for your solution up to this point.

Good luck.
Aug 25 '10 #5
jhardman
3,405 Expert 2GB
your form needs an ID, it can be the same as the name. By the way, firefox shows javascript errors in the error console (tools-> error console, or shift+control+j) and firefox is the one I would use as the standard.

Jared
Aug 26 '10 #6
First of all, I should thank you again for your great help. Especially about FireFox Error Console. It was really helpful to me. I could track the error in FireFox.

I changed the code and added an ID for my Form as follows:

Expand|Select|Wrap|Line Numbers
  1. <form method="Post" name="frm" id="frmMain">

and this is your useful code with the needed modifications:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.   <!--
  3.   function Submitform(Number) {
  4.   document.getElementByID["frmMain"].action = "AddItem.asp?idnum=<%=IdNumber%>&pgnum=" + Number;
  5.   document.getElementByID["frmMain"].submit();
  6.   }
  7.   -->
  8. </script>
  9.  

But, when I run the program, the buttons don't work on my page. Thanks to your help, I could track the error. FireFox shows the following error in the Error Console dialog box:

Expand|Select|Wrap|Line Numbers
  1. Error: document.getElementByID is undefined
  2. Source File: http://localhost/default.asp?idnum=3&pgnum=
  3. Line: 36

and the CODE in Line 36 is as follows:

Expand|Select|Wrap|Line Numbers
  1. document.getElementByID["frmMain"].action = "AddItem.asp?idnum=3&pgnum=" + Number;

Thank you very much for your support.
Aug 26 '10 #7
jhardman
3,405 Expert 2GB
sorry, if there is one thing I hate about javascript it is case sensitivity. I hold down the shift button too long, and I get nothing but errors!

getElementById is correct, not getElementByID

Jared
Aug 27 '10 #8
I did the same as you told me to do in the previous post.

I changed getElementByID to getElementById, but nothing changed. The same error, but with a small difference. Simply, the buttons do not respond to click, as if they don't exist at all. When I click on the buttons in FireFox, nothing happens, but the same error in line 36 is still visible.

Expand|Select|Wrap|Line Numbers
  1. Error: document.getElementById.frmMain is undefined

Maybe I have confused you. I can put more of the code here, but I'm sure that no other part of the program uses VBScript. I have fixed a similar code a few months ago, but about this code, due to the lack of knowledge in JavaScript, I'm still helpless.

Thank you anyway. You've been so kind, jhardman.
Aug 28 '10 #9
Dear jhardman,

I searched the Internet to find the reason of the above Error in Line 36. I found this explanation:
My rule of thumb... If you have a FORM tag, then don't use getElementById for form elements. Form elements all require a name attribute in order to be submitted to the server anyway. The FORM tag, itself, does not. Thus, whether you use a name attribute for your FORM tag or an id attribute will be dictated by whether you're using an HTML, XHTML, or XML document (or, whatever).
I got the above definition from the following link:

http://www.webdeveloper.com/forum/sh...d.php?p=596345

Based on the above information, I decided to change the code like this:
Expand|Select|Wrap|Line Numbers
  1.   document.getElementsByName["frmMain"].action = "AddItem.asp?idnum=<%=IdNumber%>&pgnum=" + Number;
  2.   document.getElementsByName["frmMain"].submit();
But it failed again.

I wonder if it can help you find a solution to my problem.

Thank you again.
Aug 28 '10 #10
I have thought a great deal over this confusing problem and finally, decided to change your efficient code to reveal something I coudn't understand.

If we change your code like this:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.   <!--
  3.   function Submitform(Number) {
  4.   var x=document.getElementsByName("frmMain");
  5.   alert(frmMain.Id);
  6.   }
  7.   -->
  8. </script>
We can see your JavaScript Function runs perfectly, but something is wrong with the Form. I don't know what it is, but by changing your JavaScript Function to the above code, when we press the buttons, an "Alert Dialog" appears and shows this message: "undefined"

I'm doing my best to find the solution. Thank you everyone for your help.
Aug 28 '10 #11
jhardman
3,405 Expert 2GB
show me the form code, at least the start tag and end tag.

Jared
Sep 2 '10 #12
Expand|Select|Wrap|Line Numbers
  1.         <td width=650 valign="top" align="center">
  2.             <form method="Post" name="frmMain" id="frmMain">
  3.  
  4.             <%
  5.             While Not RS.EOF AND ItemNumber < ItemsPerPage
  6.                 ItemNumber = ItemNumber + 1
  7.                 If rs("Verified") = True Then IsVerified = True Else IsVerified = False
  8.                 %> 
  9.  
  10.                 <table border="1" cellpadding="0" cellspacing="0" style="border-style:solid; border-collapse: collapse;" bordercolor="#DDE6CC" width="100%" dir="ltr">
  11.                     <tr height="40">
  12.                         <% If IsVerified = False Then %>
  13.                                     <td width="10%" style="font-family: Verdana; font-size: 8pt; color: #FFFFFF" bgcolor="#315973" background="http://MyDomain.com/images/catHead.gif" dir="ltr" align="center">
  14.                                     <% If Session("login") = "IsAllowed" Then Response.Write CLng(Cur * ItemsPerPage) - ItemsPerPage + ItemNumber %>&nbsp;</td>
  15.                             <% Else %>
  16.                                     <td width="10%" style="font-family: Verdana; font-size: 8pt; color: #000000" bgcolor="#315973" background="http://MyDomain.com/images/titleRow.gif" dir="ltr" align="center">
  17.                                     <% If Session("login") = "IsAllowed" Then Response.Write CLng(Cur * ItemsPerPage) - ItemsPerPage + ItemNumber %>&nbsp;</td>
  18.                         <% End If %>
  19.  
  20.  
  21.                         <% If IsVerified = True OR Session("login") = "IsAllowed" Then %>
  22.                             <% If rs("NameDirection") = "RTL" Then %>
  23.                                         <td width="80%" style="font-family: Tahoma; font-size: 9pt; color: #002200" bgcolor="#315973" background="http://MyDomain.com/images/titleRow.gif" dir=rtl align="center">
  24.                                         <%=rs("Name")%>&nbsp;</td>
  25.                                 <% Else %>
  26.                                         <td width="80%" style="font-family: Tahoma; font-size: 9pt; color: #002200" bgcolor="#315973" background="http://MyDomain.com/images/titleRow.gif" dir=ltr align="center">
  27.                                         <%=rs("Name")%>&nbsp;</td>
  28.                             <% End If %>
  29.                         <% End If %>
  30.  
  31.                         <% If IsVerified = False AND Session("login") <> "IsAllowed" Then %>
  32.                                 <td width="80%" style="font-family: Tahoma; font-size: 9pt; color: #FFFFFF" bgcolor="#315973" background="http://MyDomain.com/images/catHead.gif" dir=LTR align="center">
  33.                                 <Font Color=White>
  34.                                     <% Response.Write "<B>This item is not ready yet!</B>" %>
  35.                                 </Font>
  36.                             &nbsp;</td>
  37.                         <% End If %>
  38.  
  39.  
  40.                         <% If IsVerified = False Then %>
  41.                                     <td width="10%" style="font-family: Tahoma; font-size: 8pt; color: #FFFFFF" bgcolor="#315973" background="http://MyDomain.com/images/catHead.gif" dir="LTR" align="center">
  42.                                     <% If Session("login") = "IsAllowed" Then Response.Write RS("ID") %>&nbsp;</td>
  43.                             <% Else %>
  44.                                     <td width="10%" style="font-family: Tahoma; font-size: 8pt; color: #000000" bgcolor="#315973" background="http://MyDomain.com/images/titleRow.gif" dir="LTR" align="center">
  45.                                     <% If Session("login") = "IsAllowed" Then Response.Write RS("ID") %>&nbsp;</td>
  46.                         <% End If %>
  47.                     </tr>
  48.  
  49.  
  50.                     <tr>
  51.                         <td width="100%" style="" colspan="3" align=center>
  52.                             <!-- Begin Table of Goods -->
  53.                                 <table border="0" cellpadding="0" cellspacing="0" height=185 style="border-collapse: collapse" bordercolor="#000000" bgcolor="#EFEFE7">
  54.                                 <tr>
  55.  
  56.                                     <td width="120" height="185" style="font-family: Tahoma; font-size: 10pt; padding-top: 6" valign="top" dir="rtl" align="center">
  57.                                             <img border="0" src="<% If RS("PicPath") <> "" Then Response.Write "./Archive/" & RS("PicPath") Else : Response.Write "./images/NoPreview.jpg" %>" width="100" height="140">
  58.  
  59.                                         <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#000000">
  60.                                             <tr height=4>
  61.                                                 <td width=80></td>
  62.                                             </tr>
  63.  
  64.                                             <tr height=25>
  65.                                                     <td width=98 style="border-style: solid; border-width: 1; font-family: Tahoma; font-size: 10pt" valign="middle" align="center"  bgcolor="lightgreen">
  66.                                                     <%=RS("Price")%>&nbsp;Rials
  67.                                                 </td>
  68.                                             </tr>
  69.                                         </table>
  70.  
  71.                                     </td>
  72.  
  73.                                     <td width=530 valign="top">
  74.                                         <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#000000" dir="LTR">
  75.                                             <tr height="45" bgcolor="#EFEFE7">
  76.                                                 <td width="100" style="padding-right: 10; font-family: Tahoma; font-size: 9pt" dir="LTR" align="right" bgcolor="#EFEFE7">
  77.                                                     <% If IsVerified <> False Then %>    
  78.                                                         <b>Number of Selected Items :</b>
  79.                                                     <% End If %>
  80.                                                 </td>
  81.  
  82.                                                 <td width="425" style="padding-right: 10; padding-left: 10" align="center" bgcolor="#EFEFE7">
  83.                                                     <% If IsVerified <> False Then %>    
  84.                                                             <input name="T<%=RS("ID")%>" value="<%=Request.QueryString(CStr("T" & RS("ID")))%>" size="1" style="border-style:solid; border-width:1px; width: 35; float: right; font-family:Tahoma; font-size:10pt; text-align:center; background-color:#FFFFFF; height:22" maxlength="2" dir="ltr">
  85.                                                             <input type="button" value="Add to the Selected List" name="btnApply" style="Cursor: Pointer; Cursor: Hand; border: solid; border-width: 1; padding:0; width: 115; font-family: Tahoma; font-size:9pt; height:25; float:left; background-color:#FFCC00;" dir="LTR" language="javascript" onclick="Submitform(<%=Cur%>)">
  86.                                                     <% End If %>
  87.                                                 </td>
  88.                                             </tr>
  89.  
  90.  
  91.                                             <tr height="20" bgcolor="#EFEFE7">
  92.                                                 <% If IsVerified = True OR Session("login") = "IsAllowed" Then %>
  93.                                                         <% If RS("InfoDirection") = "RTL" Then %>
  94.                                                                     <td width="100%" style="padding-right: 10; padding-left: 10; line-height: 200%; font-family: Tahoma; font-size: 9pt" colspan=2 dir="rtl" align="justify">
  95.                                                                     <%=RS("Info")%>
  96.                                                                 </td>
  97.                                                             <% Else %>
  98.                                                                     <td width="100%" style="padding-right: 10; padding-left: 10; line-height: 200%; font-family: Tahoma; font-size: 9pt" colspan=2 dir="ltr" align="justify">
  99.                                                                     <%=RS("Info")%>
  100.                                                                 </td>
  101.                                                         <% End If %>
  102.                                                 <% End If %>
  103.  
  104.                                                 <% If IsVerified = False AND Session("login") <> "IsAllowed" Then %>
  105.                                                             <td width="100%" style="padding-right: 10; padding-left: 10; line-height: 200%; font-family: Tahoma; font-size: 9pt" colspan=2 dir="LTR" align="justify">
  106.                                                             <Font Color=Red>
  107.                                                                 <B><% 'Response.Write "<br><br><br><br>" & "This item is not ready yet!" %></B>
  108.                                                             </Font>
  109.                                                         </td>
  110.                                                 <% End If %>
  111.                                             </tr>
  112.  
  113.                                         </table>
  114.                                     </td>
  115.  
  116.                                 </tr>
  117.  
  118.                             </table>
  119.                             <!-- End Table of Goods -->
  120.                         </td>
  121.                     </tr>
  122.                 </table>
  123.  
  124.  
  125.                 <% If Session("login") = "IsAllowed" Then %>
  126.  
  127.                     <% Response.Cookies("UserAdmin") = "IsAllowed" %>
  128.                     <table width="100%" dir="LTR">
  129.                         <tr height=50>
  130.                             <td width=50% align=right>
  131.                                 <% If RS("Details") <> "" Then %>
  132.                                         <input type="button" value="More Info ..." name="btnMore" style="border: solid; border-width: 1; padding:0; cursor:hand;width: 117; font-family: Tahoma; font-size:9pt; height:25; float:right; background-color:#CCFF66; " dir="LTR" language="javascript" onclick="window.open('details.asp?ID=<%=RS("ID")%>','newwin','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,left=50,width=800')">
  133.                                 <% End If %>
  134.                             </td>
  135.  
  136.                             <td width=50% align=left style="padding-left: 30">
  137.                                 <A href=<% Response.Write "admin/product_edit.asp?id=" & rs("ID")%> target="_blank">
  138.                                     <img border='0' src='Admin/Edit.gif' width='51' height='23' align="middle"></A>
  139.                             </td>
  140.                         </tr>
  141.                     </table>
  142.  
  143.                 <% Else %>
  144.  
  145.                     <% Response.Cookies("UserAdmin") = "" %>
  146.  
  147.                     <% If RS("Details") <> "" AND IsVerified <> False Then %>
  148.                         <table><tr><td></td></tr></table>
  149.                             <input type="button" value="More Info ..." name="btnMore" style="border: solid; border-width: 1; padding:0; cursor:hand;width: 117; font-family: Tahoma; font-size:9pt; height:25; float:right; background-color:#CCFF66; " dir="LTR" language="javascript" onclick="window.open('details.asp?ID=<%=RS("ID")%>','newwin','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,left=50,width=800')">
  150.                     <% End If %>
  151.  
  152.                 <% End If %>
  153.  
  154.                 <br><br>
  155.  
  156.  
  157.                 <%
  158.                 RS.MoveNext
  159.             Wend
  160.             %>
  161.             </form>
  162.         </td>



Lines 02, 85 and 161 contain the codes for the Form and the Button that calls the SubmitForm Function. I have formatted them as Underlined, Bold and Italicized to attract your attention to them.

Thank you again.
Sep 2 '10 #13
jhardman
3,405 Expert 2GB
I just tested this, it worked fine. There were a couple mistakes I had made in the original code I posted, notice the parentheses instead of brackets.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.     <title>Test javascript function</title>
  6.  
  7.  
  8.  
  9.     <script type="text/javascript" language="javascript">
  10.       <!--
  11.         function Submitform() {
  12.  
  13.             document.getElementById("frmMain").action = "requesttest.asp";
  14.             document.getElementById("frmMain").submit();
  15.         }
  16.  
  17.         function changeHeader() {
  18.             alert(document.getElementById("h1").innerHTML);
  19.             document.getElementById("h1").innerHTML = "header is changed";
  20.             //alert(document.getElementById("frmMain").action + "'");
  21.             //document.getElementById("frmMain").action = "requesttest.asp";
  22.             //alert(document.getElementById("frmMain").action + "'");
  23.         }
  24.       -->
  25.     </script>
  26.  
  27.  
  28. </head>
  29. <body onload="changeHeader()">
  30. <h1 id="h1">Here's the main header</h1>
  31. <form method="Post" name="frmMain" id="frmMain" action="">
  32. <img alt="click here to submit" src="jhdotcomLogo.png" onclick="Submitform()"
  33. border="2" height="50" width="50" id="myPic"/>
  34. <input type="text" name="testText" />
  35. </form>
  36. </body>
  37. </html>
  38.  
Jared
Sep 2 '10 #14
Thank you a million times, jhardman. You're a genius. I owe you more than one.

I replaced the brackets with the parentheses in the JavaScript Function you specified. It solved the problem.

You've been so kind and helpful.

Thank you again.
Sep 3 '10 #15

Post your reply

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

Similar topics

8 posts views Thread by Stefan Burger | last post: by
1 post views Thread by tnhoe | last post: by
5 posts views Thread by Vyz | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.