javascript document.lastModified无法正常工作

javascript document.lastModified not working properly

本文关键字:工作 常工作 document lastModified javascript      更新时间:2023-09-26

我刚刚尝试了document.lastModified,但它返回的是系统日期和时间,而不是预期的html文件上次修改的日期和时间。

这是非常奇怪的行为。

我是如何检查的:我已经将命令插入到js中,然后在调试中断/断点模式下使用谷歌chrome dev工具在运行时查找文档属性:每次我将鼠标指向document.lastmodified属性时,它都会随着系统时间的变化而相应地变化,并每秒更新一次,而不退出html文档,也不将其保存在其他地方,也不重新加载到浏览器中,而不需要任何编辑对其进行任何其他操作。

然后我停用了断点,让代码自由执行,它也返回系统时间作为第一次检查。。。

这个双重检查让我认为该命令不再从浏览器中正确解释。

命令document.lastModified在某种程度上"坏了"吗?

编辑:我所说的文件是本地而不是服务器文件,因此不涉及http请求或标头。

。。。

根据规范:

文档的源文件的上次修改日期和时间必须源自所使用的网络协议的相关功能,例如,源自文档的HTTP last Modified标头的值,或源自文件系统中本地文件的元数据

所以它应该在那里。这是Chrome中的一个报告错误。搜索Chromium源代码表明情况确实如此(注意FIXME注释(:

// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-lastmodified
String Document::lastModified() const
{
    DateComponents date;
    bool foundDate = false;
    if (m_frame) {
        if (DocumentLoader* documentLoader = loader()) {
            const AtomicString& httpLastModified = documentLoader->response().httpHeaderField(HTTPNames::Last_Modified);
            if (!httpLastModified.isEmpty()) {
                date.setMillisecondsSinceEpochForDateTime(convertToLocalTime(parseDate(httpLastModified)));
                foundDate = true;
            }
        }
    }
    // FIXME: If this document came from the file system, the HTML5
    // specificiation tells us to read the last modification date from the file
    // system.
    if (!foundDate)
        date.setMillisecondsSinceEpochForDateTime(convertToLocalTime(currentTimeMS()));
    return String::format("%02d/%02d/%04d %02d:%02d:%02d", date.month() + 1, date.monthDay(), date.fullYear(), date.hour(), date.minute(), date.second());
}

Firefox和IE似乎已经实现了这一点:(