Tuesday, March 17, 2009

Reddit and Digg icons on Blogger without external scripts

It took me a while to be able to add reddit and Digg icons that didn't load external scripts - Blogger's templating language had me stumped. I used reddit's, Digg's, and Blogger's documentation, but I would never have gotten the "onclick" escaping right without this post.

<!-- reddit button -->
<a expr:href='"http://www.reddit.com/submit?url=" + data:post.url' expr:onclick='"window.location = \"http://www.reddit.com/submit?url=\" + encodeURIComponent(\"" + data:post.url + "\"); return false"'> <img src="http://www.reddit.com/static/spreddit1.gif" title="submit to reddit" alt="submit to reddit" border="0" /> </a>
<!-- digg button -->
<a expr:href='"http://digg.com/submit?url=" + data:post.url' expr:onclick='"window.location = \"http://digg.com/submit?url=\" + encodeURIComponent(\"" + data:post.url + "\"); return false"'> <img style="background:white;" width="16" height="16" alt="Submit Story to Digg" title="Submit Story to Digg" src="http://digg.com/img/digg-guy-icon.gif"/></a>

P.S. - if you can't find the "data:post.body" element to add the buttons after, like I couldn't, see if you forgot to check the "Expand Widget Templates" checkbox under "Layout -> Edit HTML".

Friday, March 13, 2009

Prefetching JavaScript (or anything) with jQuery

While users are logging into a web site, I thought why not prefetch some JavaScript files they'll be needing on the next page?

I could load them with Ajax and this will be invisible to users (see the Even Faster Web Sites talk by Steven Souders, and his discussion of browser busy indicators).

The following seems to work, prefetching 2 files in parallel:

(function($) {
 $.ajax({ url:"/js/file1.js", cache:true, dataType:"text" });
 $.ajax({ url:"/js/file2.js", cache:true, dataType:"text" });
})(jQuery);

The 'text' dataType means jQuery won't try to evaluate the JavaScript it fetches. The 'cache' parameter defaults to 'true', but I prefer adding it.

I tested several browsers with a proxy, and the JavaScript files are cached - cool! I presume this technique would work with any resource, not just JS.

Sunday, March 8, 2009

bzr svn: 'unsupported protocol'

Update (Aug 2009): this is a workaround for a bug that's been fixed for a while in the stable bzr versions.

This is something I've banged my head against for a nontrivial while when working with bzr and a subversion repository requiring authentication:

> bzr push http://svnrepos.example.net/trunk
bzr: ERROR: Invalid http response for http://.../.bzr/branch-format: Unable to handle http code 401: Authorization Required

Hard-coding 'http+svn' used to work (and give a warning), but it doesn't in the latest versions:

> bzr push http+svn://svnrepos.example.net/trunk
bzr: ERROR: Unsupported protocol for url "http+svn://..."

There's a ticket in progress about this issue, but there's a simple workaround: specify the username in the URL with http://username@host:

> bzr push http://username@svnrepos.example.net/trunk

Success!