开发者

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 text 'this is a test' is displayed.

my code :

<html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso 8859-1" />

   <script type="text/javascript">
        var i = 0;
        var favString = "";
        var fso;

        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);
                    favorites.innerHTML += favString; // Not working !
                    favorites.innerHTML += 'test'; // Not working too !
                    favString += ":NEXT:"; //to separate favorites.
                    i++;
                }
            }
            //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);
                    }
       开发者_如何学Python         }
            }
            catch (err) {
                alert("Security settings to be modified in your browser ");
            }
        }
   </script>

   </head>

   <body onload="Import()">

      <p>this is a test</p>     <!-- Working ! -->

      <div id="favorites">
      </div>
   </body>

</html>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜