Skip to content Skip to sidebar Skip to footer

Javascript Code Not Displaying Wanted Output

I've written some code to display my favorites in IE8 but for an unknown reason I have no output on the screen despite the fact that my page is accepted by IE and that the test tex

Solution 1:

The following works for me:

var fso, favs = [];
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 ");
    }
}

Note that all I changed was the output from an element to an array named favs. I also removed the i variable, because it wasn't used. After running the script, I checked the array in the developer tools console and it contained all my favourites.


Solution 2:

If you're getting no output at all, then either fso is null in the Import method or files.AtEnd() always evaluates to false. Since you're focusing on IE here, you might consider placing alert methods in various places with values to debug (such as alert(fso);) throughout your expected code path.


Post a Comment for "Javascript Code Not Displaying Wanted Output"