简体   繁体   中英

Javascript code not accepted by validator (JSHint)

I've written some code to display my bookmarks in IE8. To check it I've used JSHint and I get the following errors :

  • var files=new Enumerator(FavFolder.Files); 'Enumerator' is not defined. (Line 14)
  • Enumerator(FavFolder.SubFolders); 'Enumerator' is not defined. (Line 34)
  • ActiveXObject("Scripting.FileSystemObject"); 'ActiveXObject' is not defined. (Lines 46)
  • ActiveXObject("WScript.Shell"); 'ActiveXObject' is not defined. (Line 50)

Does someone know why ?

my code :

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

Enumerator and ActiveXObject are globals that are defined externally. You can tell JSHint to ignore these by putting the folllowing at the top of your JavaScript:

/*global Enumerator: false, ActiveXObject: false */

jslint is commonly for javascript in all browsers, not IE specified, so Enumerator and ActiveXObject or other browser specified objects are not supported.

If you are developing for IE only, just ignore these warnings.

Also, you may add Enumerator, ActiveXObject in the Predefined textbox.

I don't see Enumerator defined anywhere, and ActiveXObject is a Microsoft extension. ( Enumerator may be as well, at least in Windows Scripting Host.) JSHint checks against the specification, not vendor-specific additions.

They're both specific to IE, maybe the parser has an "IE" flag? Other than that you'll need to define them somehow if you really want your code to pass.

I dont if this is the correct way of doing, but this works for me

"predef": ["XDomainRequest","ActiveXObject"]

I added this to my .jshintrc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM