How to pass javascript array to vbscript?
waiming | Posted 8:34am 4. November 2002 Server Time |
I am attempting to pass a javascript array of strings to vbscript.
I found an example which passes a single string by using a hidden form.
The form variables are set in the javascript(form.formfieldname.value=astring), and then the submit() event is called on the form.
The form then calls the asp page, and uses request.querystring("IDValue") to obtain the string value.
What I want to do is instead of a single string, I want to pass an array of strings. What is happening is the request.querystring only grabs the first value in the array.
|
Frettmaestro | Posted 9:02am 4. November 2002 Server Time |
This is impossible unless you convert your array to a single string separated by commas or something. Create a js-loop that puts all the values in the array behind eachother with a comma between, and then when you retrieve them with asp you split it up:
<INPUT TYPE="hidden" NAME="id" VALUE="1,2,546,61">
<%
ID = Request.QueryString("ID")
ID_split = Split(ID, ",")
For i = 0 TO Ubound(ID_split)
Response.Write("<BR>" & ID_split(i))
Next
%>
Frettmaestro
carathanatos | Posted 3:13pm 4. September 2008 Server Time |
Idiot.
I know this is old, but it came up second in a google search for "pass an array from vbscript to javascript".
Use VBscript to response.write out the javascript code. I found it in the first answer, tested it, and it works for me.
You're welcome, denizens of the internet looking for answers.
hrmilo | Posted 11:15pm 16. June 2009 Server Time |
With the latest scripting engines at least you can access a jscript array element within vbscript using dot notation:
x.[0] 'access the first element of jscript array in vbscript
phazzy | Posted 2:58am 16. December 2009 Server Time |
@hrmillo - the method you say about works only if you specify the index as the exact number. It doesn't work in a loop, where i is the current index, or if you say like :
i = 1
x.[i] ' will fail
One can do this by writing a javascript class like so:
<script type="text/javascript>
function CustomArray(baseJSArray)
{
this.arr = baseJSArray;
this.length = baseJSArray.length;
this.GetElement = GetElem;
}
function GetElem(idx)
{
return this.arr[idx];
}
</script>
Then , you pass this to a VBScript function
<script type="text/vbscript">
Function loadFromByteArray(customJSArray)
Dim vbArr()
Dim propsAsAny
Dim arrLength
Dim i
arrLength = customJSArray.length
ReDim vbArr(arrLength - 1)
For i = 0 To arrLength - 1
vbArr(i) = customJSArray.GetElement(i)
Next
End Function
</script>
Of course, the easiest way to do it is to create a comma separated string of the js array values, pass it and then create a VB array by calling Split(). The downside of this approach is the for a large array, calling Split() takes forever. With the class approach, the time tends to be faster.
Reply to Post How to pass javascript array to vbscript?
|
|
|