views:

328

answers:

4

Is there a way to get the modification time of a file (either ctime or mtime should work) that is accessed locally through javascript.

I want to go to file:///home/me/mtime.html and have the javascript tell me that /home/me/file.txt was modified 2 minutes ago or something. I understand that javascript has limited file access due to security problems, but is there some trick since it is all done locally.

Thanks.

A: 

Probably through ActiveX or some other browser component that lets the user grant extended permissions to the browser, such as an HTA or through something like Google Gears.

In otherwords, "No", unless you're willing to do something non-standard.

Peter Bailey
A: 

Sorry but it's not possible with JavaScript.

Nisse
A: 

Here is some javascript using ActiveX that I think might help you out:

<script language=jscript runat=server> 
    var thisfile = <File_Path>; 
    thisfile = Server.MapPath(thisfile); 
    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    var fs = fso.GetFile(thisfile); 
    var dlm = fs.DateLastModified; 
    Response.Write("Last modified: " + dlm); 
</script>

If you need how long ago it was modified you would need some other javascript to subtract dlm from the current time.

Denis Sadowski
thanks, that is what I want, but ActiveX is probably the problem here as the users will be using firefox or safari.
plor
+1  A: 

Firefox has a set of components under its XPCOM (ActiveX competitor technology) that could be used to do the same thing.

Probably something like this (untested):

function getLastModifiedTime(filePath) 
{
    try 
    {
     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } 
    catch (e) 
    {
     throw new Error("Permission to read file was denied.");
    }

    var file = Components.classes["@mozilla.org/file/local;1"]
 .createInstance(Components.interfaces.nsILocalFile);

    file.initWithPath( filePath );

    return file.lastModifiedTime;
}

As for Safari.... no idea. Maybe a signed java applet?

Denis Sadowski
cool, I think if I can get this to work Safari won't be an issue. Thanks again.
plor