Javascript代码不显示想要的输出

时间:2011-05-11 10:17:51

标签: javascript internet-explorer internet-explorer-8 activex

我已经编写了一些代码来显示我在IE8中的收藏夹但由于未知原因我在屏幕上没有输出,尽管我的页面被IE接受并且显示测试文本'这是一个测试'

我的代码:

<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);
                    }
                }
            }
            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>

2 个答案:

答案 0 :(得分:1)

以下适用于我:

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 ");
    }
}

请注意,我更改的是从元素到名为favs的数组的输出。我还删除了i变量,因为它没有被使用。运行脚本后,我在开发人员工具控制台中检查了数组,它包含了我最喜欢的所有内容。

答案 1 :(得分:0)

如果您根本没有输出,则fso方法中的Import为空,或files.AtEnd()始终为false。由于您在这里专注于IE,因此您可以考虑在各个位置放置警报方法,并在整个预期的代码路径中调试值(例如alert(fso);)。