最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass an array from VBScript to JavaScript? - Stack Overflow

programmeradmin1浏览0评论

I have a webpage where I am fetching the name of files in a Folder into an array using VBScript, then I am passing that array to JavaScript variable, so that I can display the names on the screen.

VBScript Code:

Function allFiles()
    Dim arr, arr2, oTargetFolder
    arr = array()

    set oFSO = CreateObject("Scripting.FileSystemObject")
    oTargetFolder = "C:\Users\msiddiq1\Documents\WSDLs"

    set objFolder = oFSO.GetFolder(oTargetFolder)

    set oFiles = objFolder.Files

    For Each files in oFiles
        ReDim Preserve arr(UBound(arr) + 1)
        arr(UBound(arr)) = files.Name       
    Next

    allFiles = arr
End Function

JS:

var folderFiles = allFiles();
alert(folderFiles.length); // alerts `undefined`

I can pass hardcoded values from vbscript to javascript, but not this array.

Please suggest.

I have a webpage where I am fetching the name of files in a Folder into an array using VBScript, then I am passing that array to JavaScript variable, so that I can display the names on the screen.

VBScript Code:

Function allFiles()
    Dim arr, arr2, oTargetFolder
    arr = array()

    set oFSO = CreateObject("Scripting.FileSystemObject")
    oTargetFolder = "C:\Users\msiddiq1\Documents\WSDLs"

    set objFolder = oFSO.GetFolder(oTargetFolder)

    set oFiles = objFolder.Files

    For Each files in oFiles
        ReDim Preserve arr(UBound(arr) + 1)
        arr(UBound(arr)) = files.Name       
    Next

    allFiles = arr
End Function

JS:

var folderFiles = allFiles();
alert(folderFiles.length); // alerts `undefined`

I can pass hardcoded values from vbscript to javascript, but not this array.

Please suggest.

Share Improve this question asked May 11, 2015 at 9:53 EjazEjaz 1,6424 gold badges28 silver badges54 bronze badges 1
  • Just return a ma separated string instead of array. Split it inside JavaScript. – Salman Arshad Commented May 11, 2015 at 10:15
Add a ment  | 

1 Answer 1

Reset to default 8

You have to wrap the resulting array in a VBArray object and call toArray:

var folderFiles = new VBArray(allFiles());
var ff = folderFiles.toArray();
alert(ff.length);

or in one line:

var folderFiles = (new VBArray(allFiles())).toArray();

Note that VBScript is deprecated in IE11 edge mode, so it will be disappearing at some point.

发布评论

评论列表(0)

  1. 暂无评论