2012-07-21 115 views
0

我想要使用JavaScript更新远程URL的最后日期。我目前想出了这个:使用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'更改为“undefined”。代码应该将url内容加载到一个框架中,然后使用document.lastModified函数获取最后修改的日期。

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

谢谢! Josh

+0

outUpdate是什么变量?它是全球性的还是你打算通过这个功能返回它? – 2012-07-21 13:32:53

+0

@GrzegorzKaczan它是一个全局变量,稍后会用到。 – Josh 2012-07-21 13:38:01

回答

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.contentDocument.lastModified; 
+0

感谢您的描述性回复!我已经相应地更改了代码,但是outUpdate现在取得了“[object HTMLSpanElement]”的值[ ]我需要它来获取修改日期的值? – Josh 2012-07-21 13:51:05

+1

你必须用'outUpdate = oSpan.textContent'或'outUpdate = oSpan.innerHTML'替换'outUpdate = oSpan'。 – mabbas 2012-07-21 13:54:30

+0

谢谢!但是,它似乎是返回确切的当前日期和时间,而不是修改日期? – Josh 2012-07-21 14:00:55

1

为什么在try catch语句中?你期望它会抛出任何错误吗?因为你基本上依靠那个。你是否为outUpdate设置了初始值?它是否曾经进入捕获声明? 为什么你必须在这里函数getlastmod()和getLastModified()? 当你将它设置为会发生什么:

var outUpdate = "init"; 
... 

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

如果你不打算显示的页面,你可以通过使用XHR节省自己的一些带宽替换该行

oIFrame.document.lastModified; 


这里的东西,让你去...

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