Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

How do I correct this Last Modified script so it works on Safari/Chrome the way it does in IE/Firefox?

In IE and Firefox this script works the way I want it to - showing a date such as 11/08/08.  On Chrome or Safari it just displays "Sun,".  I am not very familiar with JavaScript - what do I need to change to fix this?
<script type="text/javascript">
<!--
lastmod = document.lastModified     // get string of last modified date
lastmoddate = Date.parse(lastmod)   // convert modified string to date
if (lastmoddate == 0) {               // unknown date (or January 1, 1970 GMT)
document.writeln(" Last Modified: Unknown")
} else {
document.writeln(" " + lastmod.split(" ")[0]);
}// -->
</script>

Open in new window

0
maeve100
Asked:
maeve100
  • 3
  • 2
1 Solution
 
quincydudeCommented:
document.lastModified is not a W3C standard so it may not be function in every browser
0
 
quincydudeCommented:
Hi, I have made a AJAX version that chrome also supports. But your browser must support AJAX (which most of them do)
Hope this helps.

<html>
<head>
 
<script type="text/javascript">
var xmlhttp;
 
function loadXMLDoc(url){
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{// all modern browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// for IE5, IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}
 
function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
   {// 200 = "OK"
	
	lastmod = xmlhttp.getResponseHeader('Last-Modified');		 
	lastmoddate = Date.parse(lastmod);   // convert modified string to date
	if ( lastmod == null || lastmod == "" || lastmoddate == 0) {     // unknown date (or January 1, 1970 GMT)
	alert(" Last Modified: Unknown");
	} else {
	alert(" Last Modified:" + lastmod);
	}
 
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}
 
 
 
</script>
</head>
<body>
<p id="me"></p>
<button onclick="loadXMLDoc(document.URL);">Get "Last-Modified"</button>
 
</body>
</html>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
Wouldn't a HEAD be enough?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Michel PlungjanIT ExpertCommented:
Anyway, judging from your post you get another format than your script expects.

Try this


<script type="text/javascript">
<!--
var lastmod = document.lastModified     // get string of last modified date
document.write(" Last Modified: ")
if (lastmod == 0) {               // unknown date (or January 1, 1970 GMT)
  document.writeln("Unknown")
}
else {
  var lastmoddate = new Date(Date.parse(lastmod));
  var yyyy = lastmoddate.getFullYear();
  var mm = lastmoddate.getMonth()+1;
  var dd = lastmoddate.getDate();
  if (mm<10) mm="0"+mm;
  if (dd<10) dd="0"+dd;
  document.writeln(yyyy +'/'+mm+'/'+dd);
}// -->
</script>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
Interestingly this script does not work on my site in safari
http://plungjan.name/ee/lastmod.html

but does here

http://homepage.mac.com/frank.ia.us/Sites/skl/skl.htm
0
 
maeve100Author Commented:
Thank you for the help - sorry for the delay, I was having trouble with my server and was unable to test for a few days.  It works fine for me in both Safari and Chrome.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now