Javascript在IE上返回空数组(不应该)

时间:2011-05-12 09:19:33

标签: javascript arrays internet-explorer internet-explorer-8 bookmarks

我写了一些代码来在IE8上显示我的书签。代码工作正常,除了我应该拥有的收藏夹数组(“favs”),因为输出存在但是为空(当我在开发人员工具控制台中添加“favs”时,我得到{...})。

var fso, favs = [];
var favString="";
function GetFavourites(Folder) {
    var FavFolder = fso.GetFolder(Folder);
    //Gets Favourite Names & URL's for given folder.
    var files = new Enumerator(FavFolder.Files);
    for (; !files.atEnd(); files.moveNext()) {
        var fil = files.item();
        if (fil.Type == "Internet Shortcut") {
            var textReader = fso.OpenTextFile(fil.Path, 1, false, -2);
            var favtext = textReader.ReadAll();
            var start = favtext.indexOf("URL", 16);
            var stop = favtext.indexOf("\n", start);
            favString = fil.Name.replace(/.url/, "");
            favString += ":URL:";
            //to separate favourite name & favorite URL
            favString += favtext.substring(start + 4, stop - 1);
            favs.push(favString);
        }
    }
    //Checks any subfolder exists
    var subfolders = new Enumerator(FavFolder.SubFolders);
    for (; !subfolders.atEnd(); subfolders.moveNext()) {
        var folder = subfolders.item();
        GetFavourites(folder.Path);
    }
}
function Import() {
    try {
        fso = new ActiveXObject("Scripting.FileSystemObject");
        if (fso !== null) {
            //Create windows script shell object to access Favorites folder in user system.
            var object = new ActiveXObject("WScript.Shell");
            var favfolderName = object.SpecialFolders("Favorites");
            if (favString === "") {
                GetFavourites(favfolderName);
            }
        }
    }
    catch (err) {
        alert("Security settings to be modified in your browser ");
    }
}

1 个答案:

答案 0 :(得分:1)

我发现我的错误在哪里:我忘了我在法语中使用IE所以测试“if(fil.Type ==”Internet Shortcut“)”不起作用;我必须用法语等同的“Raccourci Internet”替换“Internet Shortcut”。 : - ))