one year agoUpdate to addSizes (v0.3)

I have released version 0.3 of the addSizes script with various fixes and more file types. The code now looks at links to documents of the following types: pdf, doc, zip, mp3, ogg, m4u, jpg, png, swf. If you are using the script and want to add your own, feel free—let me know though, I would be interested to see how people use it.

Thanks to Marko Mrdjenovič for pointing out in a comment on the previous entry that I should in fact be using encodeURIComponent() instead of encodeURI() before sending the URL to json-head. It turns out encodeURI() assumes it is given an entire URL and does not escape & and other reserved characters.encodeURIComponent() on the other hand makes no such assumption and will escape all characters in the given string. Since json-head needs to take a fully escaped URI in order to get the head data, encodeURIComponent() should work better.

In the original entry I should have stated more explicitly that this script will only work properly with files json-head can see. They need to be publicly visible on the web, ie files behind https may not behave as expected. I am still working on methods of getting around the issue with protected files.

A major fix to this version is that the code now works with relative file paths. Bizarly the issue relates to how I was getting the href of the link. With the previous versions I was using jQuery to get the href, $(this).attr('href'), for relative file paths this returns the exact value, eg "media/test.doc". Changing this to use this.href, fixes the issue as it actually interpretes the link to get the absolute URL. Magic.

The code is released under the BSD license.

5 comments

  1. Excellent plugin, thanks.

    When I tried to implement it I got an error. I'm not a javascript expert, but the error had something to do with the "console.log(url);" line. I removed this and the script works like a charm.

    Kevin 30th September 2008 18:33permalink.

  2. Thank you very much for sharing this awesome script!

    Pieter 7th October 2008 09:47permalink.

  3. We have links to lots of documents for research that are hosted on other sites. Since some of their file extensions are in CAPS, I added each extension in the list with a CAPS alternate. Otherwise, the json callback doesn't recognize the file, since it's looking for the case-sensitive extension.

    Thanks to Kevin for the "console.log(url)" heads up. My debugger just kept telling me that "console" was undefined, but once I removed that line it worked fine.

    This script has come in real handy for our members. Thanks for your hard work!

    Morgan 7th April 2009 15:35permalink.

  4. I'm desperately trying to get this working but even the sample on this site doesn't work, for a week now. Does anyone know why? Thanks, it looks like a great script if we can get it working! Perhaps it is a json problem?

    AFL 4th February 2010 15:11permalink.

  5. I guess that to receive the <a href="http://lowest-rate-loans.com/topics/home-loans">lowest-rate-loans.com</a> from banks you ought to have a good motivation. However, once I've got a commercial loan, just because I was willing to buy a bike.

    SabrinaHINES28 20th June 2010 00:07permalink.

Line breaks are preserved; URLs will be converted in to links.

Enter your own, valid XHTML. Allowed tags are: a, p, blockquote, ul, ol, li, dl, dt, dd, em, strong, dfn, code, q, samp, kbd, var, cite, abbr, acronym, sub, sup, br, pre

18th September 2008

You are reading "Update to addSizes (v0.3)" written by Natalie Downe on the 18th of September 2008 at 12:26 am.

Next: Talk like a pirate, walk like a pirate

Previous: dConstruct and my sisters graduation, a busy week!