简体   繁体   English

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

[英]Get remote url's last modified date with javascript

I am trying to get the last date that a remote url was updated using javascript. 我试图获取使用javascript更新远程URL的最后日期。 I have currently come up with this: 我目前想出这个:

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);}
}

However, this code seems to always change 'outUpdate' to "undefined". 但是,此代码似乎总是将“outUpdate”更改为“undefined”。 The code is supposed to load the url contents into a frame and then use the document.lastModified function to get the last modified date. 该代码应该将url内容加载到一个框架中,然后使用document.lastModified函数来获取最后修改日期。

Any ideas on how to fix this? 有想法该怎么解决这个吗?

Thanks! 谢谢! Josh 玩笑

Why is it in try catch statement? 为什么它在try catch语句中? Do you expect it to throw any error? 你认为它会抛出任何错误吗? Because you basically rely on that. 因为你基本上都依赖于此。 Do you set initial value for outUpdate? 你为outUpdate设置了初始值吗? Does it ever enters catch statement? 它是否曾进入捕获声明? Why do you have to functions here getlastmod() and getLastModified()? 你为什么要在这里运行getlastmod()和getLastModified()? What happens when you set it to: 将其设置为时会发生什么:

var outUpdate = "init";
...

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

You are accessing the element oIFRAME & oSpan before adding them to your document, you have to add these 2 lines before the try block: 在将元素添加到文档之前,您正在访问元素oIFRAMEoSpan ,您必须在try块之前添加这两行:

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

The id of your iFrame is oIFRAME and not oIFrame , replace this line: 您的iFrame的ID是oIFRAME而不是oIFrame ,请替换此行:

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

By 通过

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

document is not a property of your iFrame object, contentDocument is. document不是你的iFrame对象的属性, contentDocument是。 Replace this line 替换此行

oIFrame.document.lastModified;

by 通过

oIFrame.contentDocument.lastModified;

If your not going to display the page you could save yourself some bandwidth by using xhr. 如果你不打算显示页面,你可以使用xhr节省一些带宽。
Here's something to get you going... 这是让你去的东西......

manifest.json 的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 popup.html

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

popup.js 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM