JQuery for Everyone: Dressing-up Links Pt3
In part 1, we added some nifty icons to external links. However, because we only compared the anchor’s server name to the local server name, bad links still look good. By using Simon Willison’s json-head application, we can add file size calculations like our internal links and display a “file not found” message for bad links. The only thing we can’t see are external links that require authentication (the reason we duplicated the json-head logic in AJAX to begin with).
Let’s turn this:
Into this…
We’ll extend our second function from part 2 to include scenarios for external links:
<script type="text/javascript"> function hdrDetails(i, elm, cl) { cl = cl/1024; var sz = cl>1024?"MB":"KB"; cl = cl>1024?cl/1024:cl; var len = $(elm).eq(i).text().length; if(len > 0) { $(elm).eq(i).after("<span> ("+cl.toFixed(2)+" "+sz+")</span>"); } } $(function() { var elm="a[href$='.pdf'],"+ "a[href$='.doc'],"+ "a[href$='.ppt'],"+ "a[href$='.xls'],"+ "a[href$='.docx'],"+ "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); }else{ $(elm).eq(i).after("<span> (file not found)</span>"); } } }); } if (e.hostname && e.hostname !== location.hostname) { //external link //add the icon $(this).after(' <img src="/PaulGrenier/images1/external.png" alt="external link">'); //call json-head var url= "http://json-head.appspot.com/?url="+encodeURI($(this).attr('href'))+"&callback=?"; $.getJSON(url, function(json){ if(json.ok && json.headers["Content-Length"] && json.status_code == 200) { var cl = json.headers["Content-Length"]; hdrDetails(i, elm, cl); //run the calculations and insert a file size } if(json.ok && json.status_code == 404) { //file not found error $(elm).eq(i).after("<span> (file not found)</span>"); } }); } }); }); </script>
If you have ideas for extending this further, please leave a comment.
- 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 useful. Thanks!
Unfortunately this only works with publicly accessible files, right?
@Oskar,
Right, unless you’re currently signed into another application (an active session token or cookie) this script does not try to authenticate you.
The other post explains how to use this for internal files (where the user is already authenticated).
How would I limit this to one selected web part rather than the entire page?