使用javascript获取远程网址上次修改日期

时间:2012-07-21 13:05:03

标签: javascript html url google-chrome-extension

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

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”更改为“undefined”。代码应该将url内容加载到一个框架中,然后使用document.lastModified函数来获取最后修改日期。

有关如何解决此问题的任何想法?

谢谢! 约什

3 个答案:

答案 0 :(得分:1)

为什么它在try catch语句中?你认为它会抛出任何错误吗?因为你基本上都依赖于此。你为outUpdate设置了初始值吗?它是否曾进入捕获声明? 你为什么要在这里运行getlastmod()和getLastModified()? 将其设置为时会发生什么:

var outUpdate = "init";
...

...
getlastmod("/");
console.log("outUpdate is: ", outUpdate);

答案 1 :(得分:1)

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

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

iFrame的ID为 oIFRAME ,而不是 oIFrame ,请替换此行:

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

通过

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

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

oIFrame.document.lastModified;

通过

oIFrame.contentDocument.lastModified;

答案 2 :(得分:0)

如果您不打开页面,可以使用xhr节省一些带宽 这是让你去的东西...

manifest.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();
相关问题