Sharing buttons for Twitter, Facebook, Google+ and the like are becoming a frequent request when doing web work. It’s a good ask most of the time — but often it comes at the cost of readable and/or maintainable code.
document.write() profusely. If Flickr was down, my site was down.
Today’s scripts are a lot smarter, They’re placed at the bottom of a document, and dynamically insert new scripts into the document
head to load them asynchronously. This means they don’t block the rendering of your webpage, and your site will work independent of the other service(s).
That’s an improvement, but I still don’t feel good about the huge chunks of copy/pasted code. It can be particularly bad when inheriting a project where scripts were pasted haphazardly with little knowledge of what they’re actually doing.
For the major three, I have an improvement that I cooked up using Modernizr’s load function.
The old copy/pasted scripts:
Can be replaced with one finely crafted
20 lines of code replaced with 7. A nice refactor in my opinion. And if you’re not the Modernizn’ type, you can always use the smaller underlying library, yepnope.js.
January 20, 2012