0

I am using classic .asp. I have the following:

<script language="javascript">
var JSintTotal = <% =intTotal%>;
var JSarrHotelsProdCols = Create2DArray(JSintTotal);
<%
for x = 0 to intTotal - 1
response.write  "JSarrHotelsProdCols["&x&"][0] =  "&arrHotelsProdCols(0,x)&"';" & vbCrLf
response.write  "JSarrHotelsProdCols["&x&"][1] = '"&arrHotelsProdCols(1,x)&"';" & vbCrLf
response.write  "JSarrHotelsProdCols["&x&"][2] = '"&arrHotelsProdCols(2,x)&"';" & vbCrLf
response.write  "JSarrHotelsProdCols["&x&"][3] = '"&arrHotelsProdCols(3,x)&"';" & vbCrLf
next

%>
</script>

The Create2DArray() function is working properly, I grabbed from this site.

If I comment out the for/next loop and make x=0, this will populate JSarrHotelsProdCols[0][0] through JSarrHotelsProdCols[0][3] with the correct values in arrHotelsProdCols(0,0) through arrHotelsProdCols(3,0). I check this using an alert function onDblClick after opening the .asp page.

I can also see the text written to the ‘page source’:

JSarrHotelsProdCols[0][0] = '4 MEX HOTEL & LIVING';
JSarrHotelsProdCols[0][1] = '3 STAR';
JSarrHotelsProdCols[0][2] = '2KM';
JSarrHotelsProdCols[0][3] = '8KM';

However, when I run the for/next loop I cannot see the values in the JSarrHotelsProdCols array using the same alert function, but I can see all 612 x 4 statements written out correctly in the ‘page source’.

Why is this not working when I loop through this assignment but it works when I don’t loop through it?

Let me know if you need more info, I wasn't sure how much is enough.

Thanks.

1
  • ecarrizo, anyone, the [x][0] column text sometimes contains an apostrophe as in, St. Paul's. So as I loop through the 612 rows I hit one of these cases and boom. Is there a way deal with an apostrophe or should I not allow them? Commented Jul 1, 2015 at 3:45

1 Answer 1

1

The first line inside the loop is missing an apostrophe. Change...

response.write  "JSarrHotelsProdCols["&x&"][0] =  "&arrHotelsProdCols(0,x)&"';" & vbCrLf

...to...

response.write  "JSarrHotelsProdCols["&x&"][0] =  '"&arrHotelsProdCols(0,x)&"';" & vbCrLf

You can escape apostrophes in JavaScript that may be inside your array values with a backslash, eg Replace(arrHotelsProdCols(0,x), "'", "\'"). Alternately you could replace the single quotes with double quotes (if double quotes won't exist in your array values), eg...

response.write  "JSarrHotelsProdCols["&x&"][0] =  """&arrHotelsProdCols(0,x)&""";" & vbCrLf

(note that the double quotes are duplicated as VBScript requires this if they are already inside a VBScript string)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks John. The missing apostrophe above was lost upon posting to this forum. Replacing the apostrophe with double quotes worked on the data I have. Is the Replace you mentioned a Javascript replace? I am moving values from a vbarray (arrHotelsProdCols) to a javascript array (JSarrHotelsProdCols), that Replace won't work if the array cannot be populated first?
arrHotelsProdCols(0,x) = Replace(arrHotelsProdCols(0,x), "'", "`")
The Replace is VBScript and will do the replacement on the server-side, not in your resulting client-side JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.