JQuery for Everyone: Dressing-up Links Pt2
Do you like the ease of the OOB List View Web Parts but hate the way it displays file size? Yeah, me too. I’d like to see the file size in either KB or MB right next to the file and only for client application files (like .pdf, doc., and .xls).
Let’s turn this:
into this…

I admit I got the idea from seeing Simon Willison’s json-head application, hosted on Google’s appspot. Only, Simon’s program can’t authenticate to my WSS site so I rebuilt it using jQuery and AJAX.
We need a function to dynamically tell us the size in KB or MB and a function to get the data. But we’re not using web services. Instead, we’ll use an undocumented AJAX call with jQuery, a HEAD request.
The HEAD request returns, among other things, the Content-Length (which is the file’s size in bytes) but doesn’t return the entire file. This way, we get the information we want without clogging up our tubes!
<script type="text/javascript" src="/PaulGrenier/js/jquery.js"></script> <script type="text/javascript"> function hdrDetails(i, elm, cl) { cl = cl/1024; //divide content-length by 1024 (KB) var sz = cl>1024?"MB":"KB"; //if cl is still big, set to MB cl = cl>1024?cl/1024:cl; //if cl is still big, divide again var len = $(elm).eq(i).text().length; //check the link's text length if(len > 0) { //add a file size $(elm).eq(i).after("<span> ("+cl.toFixed(2)+" "+sz+")</span>"); } } $(function() { var elm="a[href$='.pdf'],"+ //only the file types we want "a[href$='.doc'],"+ "a[href$='.ppt'],"+ "a[href$='.xls'],"+ "a[href$='.docx'],"+ //don't forget 2007 versions "a[href$='.pptx'],"+ "a[href$='.xlsx']"; $(elm).each(function(i, e) { if (e.hostname && e.hostname == location.hostname) { $.ajax({ type: "HEAD", url: $(this).attr("href"), complete: function(xhr, textStatus) { var cl=xhr.getResponseHeader("content-length"); if(textStatus=="success") { var cl=xhr.getResponseHeader("content-length"); hdrDetails(i, elm, cl); //call the calculation fn }else{ $(elm).eq(i).after("<span> (file not found)</span>"); } } }); } }); }); </script>
You may have noticed that we’re only calculating file size for internal files. That’s because AJAX, using XMLHttpRequest, can only make requests to the originating server.
So tomorrow, I’ll show you how to add Simon’s application to do the same file size calculations for external files.
- JQuery for Everyone: Accordion Left Nav
- JQuery for Everyone: Print (Any) Web Part
- JQuery for Everyone: HTML Calculated Column
- JQuery for Everyone: Dressing-up Links Pt1
- JQuery for Everyone: Dressing-up Links Pt2
- JQuery for Everyone: Dressing-up Links Pt3
- JQuery for Everyone: Cleaning Windows Pt1
- JQuery for Everyone: Cleaning Windows Pt2
- JQuery for Everyone: Fixing the Gantt View
- JQuery for Everyone: Dynamically Sizing Excel Web Parts
- JQuery for Everyone: Manually Resizing Web Parts
- JQuery for Everyone: Total Calculated Columns
- JQuery for Everyone: Total of Time Differences
- JQuery for Everyone: Fixing Configured Web Part Height
- JQuery for Everyone: Expand/Collapse All Groups
- JQuery for Everyone: Preview Pane for Multiple Lists
- JQuery for Everyone: Preview Pane for Calendar View
- JQuery for Everyone: Degrading Dynamic Script Loader
- JQuery for Everyone: Force Checkout
- JQuery for Everyone: Replacing [Today]
- JQuery for Everyone: Whether They Want It Or Not
- JQuery for Everyone: Linking the Attachment Icon
- JQuery for Everyone: Aspect-Oriented Programming with jQuery
- JQuery for Everyone: AOP in Action - loadTip Gone Wild
- JQuery for Everyone: Wiki Outbound Links
- JQuery for Everyone: Collapse Text in List View
- JQuery for Everyone: AOP in Action - Clone List Header
- JQuery for Everyone: $.grep and calcHTML Revisited
- JQuery for Everyone: Evolution of the Preview
- JQuery for Everyone: Create a Client-Side Object Model
- JQuery for Everyone: Print (Any) Web Part(s) Plugin
- JQuery for Everyone: Minimal AOP and Elegant Modularity
- JQuery for Everyone: Cookies and Plugins
- JQuery for Everyone: Live Events vs. AOP
- JQuery for Everyone: Live Preview Pane
- JQuery for Everyone: Pre-populate Form Fields
- JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
- Use Firebug in IE
- JQuery for Everyone: Extending OWS API for Calculated Columns
- JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
- JQuery for Everyone: Email a List of People with OWS
- JQuery for Everyone: Faster than Document.Ready
- jQuery for Everyone: Collapse or Prepopulate Form Fields
- jQuery for Everyone: Hourly Summary Web Part
- jQuery for Everyone: "Read More..." On a Blog Site
- jQuery for Everyone: Slick Speed Test
- jQuery for Everyone: The SharePoint Game Changer
- JQuery For Everyone: Live LoadTip
Very neat concept. I have been putting some thought into a similar functionality except I want to be able to apply this to the site level. I want to have the ability to look at a parent site and see the size of each library, list and sub-site below it. Can this concept be applied and return that result? Can this be applied to a folder to return the size of its contents?
thanks Larry
@Larry,
A HEAD request can not target the folder/container. For that information I think we have to ask the database (not supported) or build an application using the SP API (too developery). However, I think there are some codeplex projects that do this sort of thing.
Very original idea, and easy to use.
copy pasted in a content editor and it worked like a charm!
keep them coming!
i did change the link for the jquery file to http://jqueryjs.googlecode.com/files/jquery-1.2.6.js
Hi There
when i try this with a Doc Lib that is grouped by some field it doesn’t work.
Any ideas