1,804 articles and 15,139 comments as of Sunday, May 15th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Thursday, December 18, 2008

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.



Paul Grenier

View all entries in this series: PaulGrenier-JQuery for Everyone»
Entries in this series:
  1. JQuery for Everyone: Accordion Left Nav
  2. JQuery for Everyone: Print (Any) Web Part
  3. JQuery for Everyone: HTML Calculated Column
  4. JQuery for Everyone: Dressing-up Links Pt1
  5. JQuery for Everyone: Dressing-up Links Pt2
  6. JQuery for Everyone: Dressing-up Links Pt3
  7. JQuery for Everyone: Cleaning Windows Pt1
  8. JQuery for Everyone: Cleaning Windows Pt2
  9. JQuery for Everyone: Fixing the Gantt View
  10. JQuery for Everyone: Dynamically Sizing Excel Web Parts
  11. JQuery for Everyone: Manually Resizing Web Parts
  12. JQuery for Everyone: Total Calculated Columns
  13. JQuery for Everyone: Total of Time Differences
  14. JQuery for Everyone: Fixing Configured Web Part Height
  15. JQuery for Everyone: Expand/Collapse All Groups
  16. JQuery for Everyone: Preview Pane for Multiple Lists
  17. JQuery for Everyone: Preview Pane for Calendar View
  18. JQuery for Everyone: Degrading Dynamic Script Loader
  19. JQuery for Everyone: Force Checkout
  20. JQuery for Everyone: Replacing [Today]
  21. JQuery for Everyone: Whether They Want It Or Not
  22. JQuery for Everyone: Linking the Attachment Icon
  23. JQuery for Everyone: Aspect-Oriented Programming with jQuery
  24. JQuery for Everyone: AOP in Action - loadTip Gone Wild
  25. JQuery for Everyone: Wiki Outbound Links
  26. JQuery for Everyone: Collapse Text in List View
  27. JQuery for Everyone: AOP in Action - Clone List Header
  28. JQuery for Everyone: $.grep and calcHTML Revisited
  29. JQuery for Everyone: Evolution of the Preview
  30. JQuery for Everyone: Create a Client-Side Object Model
  31. JQuery for Everyone: Print (Any) Web Part(s) Plugin
  32. JQuery for Everyone: Minimal AOP and Elegant Modularity
  33. JQuery for Everyone: Cookies and Plugins
  34. JQuery for Everyone: Live Events vs. AOP
  35. JQuery for Everyone: Live Preview Pane
  36. JQuery for Everyone: Pre-populate Form Fields
  37. JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
  38. Use Firebug in IE
  39. JQuery for Everyone: Extending OWS API for Calculated Columns
  40. JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
  41. JQuery for Everyone: Email a List of People with OWS
  42. JQuery for Everyone: Faster than Document.Ready
  43. jQuery for Everyone: Collapse or Prepopulate Form Fields
  44. jQuery for Everyone: Hourly Summary Web Part
  45. jQuery for Everyone: "Read More..." On a Blog Site
  46. jQuery for Everyone: Slick Speed Test
  47. jQuery for Everyone: The SharePoint Game Changer
  48. JQuery For Everyone: Live LoadTip
 

Please Join the Discussion

8 Responses to “JQuery for Everyone: Dressing-up Links Pt3”
  1. Martin Braun says:

    Very useful. Thanks!

  2. Unfortunately this only works with publicly accessible files, right?

  3. AutoSponge says:

    @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).

  4. Walter says:

    How would I limit this to one selected web part rather than the entire page?

Trackbacks

Check out what others are saying about this post...
  1. [...] JQuery for Everyone: Dressing-up Links Pt3 [...]

  2. SharePoint Kaffeetasse #98…

    Tools und Addons Custom Content Editor Web Part for SharePoint jQuery http://jquery.com/ jQuery is a…

  3. OneOfSix says:

    Add Links to SharePoint Wiki Toolbar using jQuery…

  4. [...] JQuery for Everyone: Dressing-up Links Pt3 [...]




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!