1

我正在尝试获取使用 javascript 更新远程 url 的最后日期。我目前想出了这个:

function getlastmod(url)
{
    var ifrm = document.createElement("IFRAME"); 
    ifrm.setAttribute("src", url);
    ifrm.setAttribute("id", "oIFRAME");
    ifrm.style.display = "none"; 
    var spanTag = document.createElement("span"); 
    spanTag.id = "oSpan"; 
    try 
    {
        var oIFrame = document.getElementById("oIFrame");
        var oSpan = document.getElementById("oSpan");
        oSpan.innerHTML = oIFrame.src + " last modified " +
        oIFrame.document.lastModified;
        outUpdate=oSpan;
    } 
    catch(E) {setTimeout("getlastmod();",50);}
}

但是,此代码似乎总是将“outUpdate”更改为“未定义”。该代码应该将 url 内容加载到框架中,然后使用 document.lastModified 函数获取最后修改日期。

有想法该怎么解决这个吗?

谢谢!乔什

4

3 回答 3

1

为什么它在 try catch 语句中?你希望它抛出任何错误吗?因为你基本上依赖它。您是否为 outUpdate 设置初始值?它是否曾经进入 catch 语句?为什么你必须在这里函数getlastmod() 和getLastModified()?当您将其设置为:

var outUpdate = "init";
...

...
getlastmod("/");
console.log("outUpdate is: ", outUpdate);
于 2012-07-21T13:45:52.353 回答
1

您正在访问元素oIFRAME&oSpan在将它们添加到文档之前,您必须在try块之前添加以下 2 行:

document.body.appendChild(ifrm);
document.body.appendChild(spanTag);

你的 idiFrameoIFRAME而不是oIFrame,替换这一行:

var oIFrame = document.getElementById("oIFrame");

经过

var oIFrame = document.getElementById("oIFRAME");

document不是您的iFrame对象的属性,contentDocument是。替换此行

oIFrame.document.lastModified;

经过

oIFrame.contentDocument.lastModified;
于 2012-07-21T13:46:22.050 回答
0

如果您不打算显示该页面,您可以使用 xhr 为自己节省一些带宽。
这里有一些东西可以让你继续......

清单.json

{
  "name": "Get last modified header with XHR.",
  "version": "1.0",
  "permissions": [
    "tabs", "<all_urls>"
  ],
  "browser_action": {
      "default_title": "Get last modified header with XHR.",
      "default_icon": "icon.png",
      "default_popup": "popup.html"
  },
  "manifest_version" : 2
}

popup.html

<html>
  <head>
    <script src="popup.js"></script>
  </head>
  <body style='width : 400px;'>
    <div id="message">Getting File.....</div>
  </body>
</html>

popup.js

var xhr = new XMLHttpRequest();

xhr.open('GET', 'http://www.w3schools.com/jsref/lastmodified.htm', true);

xhr.onerror = function() {
    var message = document.querySelector('#message');
    message.innerText = 'Error getting url';
}

xhr.onreadystatechange = function() {
    // readystate 2, headers recieved
    if (this.readyState == 2){
        var message = document.querySelector('#message');
        if (this.getResponseHeader("Last-Modified")){
            message.innerText = 'Got the headers\n';
            message.innerText += 'Last Last-Modified : ' + this.getResponseHeader("Last-Modified");
        } else {
            message.innerText = 'Got the headers\n';
            message.innerText += 'But there was no Last-Modified\n';
            // If the file doesnt exist your still going to get headers
            message.innerText += 'Or there was an error in getting the file';
        }
        // Make sure you access the headers before this abort, as they wont be available afterwards
        this.abort();
    }
}

xhr.send();
于 2012-07-21T15:56:14.790 回答