5

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.

4 Answers 4

1

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?

1
  • 1
    cool, I think if I can get this to work Safari won't be an issue. Thanks again.
    – plor
    Jul 21, 2009 at 20:26
1

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.

1
  • thanks, that is what I want, but ActiveX is probably the problem here as the users will be using firefox or safari.
    – plor
    Jul 21, 2009 at 20:01
0

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.

0

Sorry but it's not possible with JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.