Heres something I knocked up for you. I'm quite proud of it as well.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript">
function locatePDFS() {
var urls = document.getElementsByTagName('A');
for(var i = 0; i < urls.length; i++) {
if(urls[i].href.toLowerCase().indexOf('pdf') > -1) {
getFileSize(urls[i].href, urls[i]);
}
}
}
function getFileSize(url, el) {
var request = createXMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
if(request.status == 200) {
var fileSize = parseInt(request.getResponseHeader('Content-Length')) / 1024;
el.innerHTML += ' (' + Math.round(fileSize) + ' kb)';
} else {
alert('ReadyState: ' + request.readyState + '\nStatus: ' + request.status);
}
}
}
request.send(null);
}
function createXMLHttpRequest() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined") {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
throw new Error("XMLHttpRequest not supported");
}
}
window.onload = locatePDFS;
</script>
</head>
<body>
<a href="http://www.ncctq.org/ModelComponentsMatrix.pdf">http://www.ncctq.org/ModelComponentsMatrix.pdf</a> <br />
<a href="http://www.rdes.it/riv3_legge210.pdf">http://www.rdes.it/riv3_legge210.pdf</a> <br />
<a href="http://www.forrest19.com/flbro.pdf">http://www.forrest19.com/flbro.pdf</a> <br />
<a href="http://www.sitepoint.com/forums/">http://www.sitepoint.com/forums/</a> <br />
<a href="http://www.domain.com/filename.PDF">http://www.domain.com/filename.PDF</a> <br />
</body>
</html>
What happens is when the page loads, it goes away and gets every link within the page. It then checks each link for the existance of pdf. If it finds a file with 'pdf' in it will then use AJAX to go away and load that file. By doing that, the response also returns a Content-Length header which tells us the size of the pdf.
Not sure whether this is the best way, as it has to load each and every PDF file to get its size, but it certainly works.
Bookmarks