javascript

You are currently browsing articles tagged javascript.

The Open Library JSON API includes an image URL in the book details. The images are lower resolution than the LibraryThing images, but a low-res image is better than none. So in cases where the book’s image is not available from LibraryThing, the BookLinker plugin will use the Open Library image if one is available.

You’re still required to supply a LibraryThing API key if you want the high-res images; but if you don’t supply the API key, you’ll still get a book cover if one is available from Open Library. The “No Image” placeholder will show only if Open Library can’t supply one.

The latency in the Open Library service is significantly higher than the LibraryThing latency; this could be due to how I’m parsing the JSON, or it could be on the service side. So there may some delay in getting the author, title, and book cover to appear in the popup (no more than a couple seconds).

That, and a little tweak to the CSS for displaying the title and author, and I’m calling it ready for testing. You can get the plugin here if you want to try it out in your own WordPress site; otherwise, it should be available in the main WordPress plugin repositories some time next week.

Let me know what you think of this plugin; leave enhancement requests and bug discoveries in the comments on the BookLinker posts or page, or drop me a line. If you have a bug to report, the more information you can provide the better: WordPress version (I’m using 2.7), browser version (so far, I’ve only tested in the current Firefox), operating system (currently working on Windows XP and Ubuntu Jaunty Jackalope), and original URL of the book link you’re using. Bugs are much more fun to squash if I can make them appear on my screen.

Tags: , , , , , ,

I’ve made a couple of small adjustments to the BookLinker plugin I announced yesterday.

  1. The popup will now be placed according to the available window width. I found that if the link appears close to the right margin of the browser screen, the popup could drift off-screen. Now, the popup’s left position will be based on the position of the link and the window width; ideally, the popup will be positioned directly above the link, but it will shift left accordingly.
  2. LibraryThing has many book cover images available, but it doesn’t have them all. If a book cover is not available, an appropriately-size “No image available” image will be loaded instead. (I actually like the way I got this to work, by registering an onLoad event on the image; because there’s some latency in the request from LibraryThing, who will send an invisible 1×1 image when no cover is available, I don’t really know until the image loads if I got anything good. Registering an onError didn’t work, since something will always load, but in the onLoad I check the image width and then swap as appropriate.)

I would really like to add an AJAX call, probably to LibraryThing, to retrieve more information about the book and display the author and title. Not as elaborate as the LibraryThing widget (you can build your own here or see mine in the left sidebar here), but a little more than blank space. This is a little beyond my PHP skills this morning, though maybe I’ll have a solution by tonight.

Tags: , , , , , ,

I’ve just written my first major WordPress plugin, BookLinker. It was developed for another of my websites, which features quite a few book reviews; the philosophical reasoning behind it is here. But it was also an interesting little glimpse into the WordPress programming paradigm.

The plugin’s goal was to make it easy for me to switch Amazon affiliate links, or augment those links, with links to other book sources. It was prompted by the “#amazonfail” fiasco of a month ago, but the need for such a tool goes deeper than one incident. I want to encourage people to buy books from other places, like Powell’s or IndieBound, or to get them from their library, but the thought of changing all those Amazon links was daunting.

My original thought was to write something that would go into each blog post with an Amazon link and change it to an IndieBound or Powell’s link. But as I looked into the variety of link formats that Amazon provides, and the number of links on my sites, that seemed a more dangerous way to go: it would be difficult to back out a data change like that.

So my second thought was to make the changes on the client side, in JavaScript. This let me do most of the heavy lifting in a language that I’m at least familiar with, using PHP just for the plugin framework and configuration, and it also makes backing things out or tweaking the functionality a lot simpler than a data change would have been.

Consequently, there’s not a lot of PHP in this plugin. All I really had to learn was how to collect, save, and retrieve user configuration variables, and then emit those variables onto the page for the JavaScript piece to take over. And from what little I saw of PHP to accomplish this, I’m glad there wasn’t much; it reminded me of the bad old days of early JSP coding, when people would plug JDBC code and huge scriptlet blocks into their pages, tiered architecture be damned. I have a new respect for people who can take a scripting language with limited tooling and turn out a robust and powerful tool like WordPress.

I like the WordPress plugin architecture. It’s very easy to use the “hooks” and extend the functionality of the core system, which makes for a rich and varied ecosystem. This is certainly one of the best arguments for open architecture as well as for open source.

The thing I like most about this plugin is the UI. I was able to reuse some concepts from the last professional project I had, and build a nice little modal window to hold the collection of links.

You can see it in action here, with this link to Effective Java (2nd Edition) (Java Series); buy it from Amazon if you must, but you can’t blame me for limiting your choices.

Tags: , , , , ,

Doing anything relatively complex in CSS while maintaining cross-browser support is the bane of web design. All of the major browsers–Firefox, Safari, Opera, and the various flavors of IE–interpret CSS in their own quirky ways (even Firefox slips on a couple of obscure CSS2 items), and none of them seem to agree on the particulars of their quirks. And since most web designers and developers can’t dictate the browser that their visitors will use–”works best in IE” or “please use Firefox” is so tacky–we need to engage in all sorts of trickery to get things to render passably well in the most common browsers.

This is especially frustrating in the case of Internet Explorer, where the differences between versions can be as great as their differences with other browsers. CSS “hacks” that exploited IE6’s failure to parse certain CSS selectors, for example, fail in IE7: or at least, the hacks fail, but the behaviors those hacks tried to mitigate are still there.

In the project I’m currently working on, I wanted to avoid muddying my CSS files with hard-to-decipher hacks. I want my CSS code to be readable and extensible, and to future-proof my design as much as possible against future browser quirks. Client-side JavaScript browser-sniffing techniques are less than reliable, since they often rely on the same kinds of “what features does this browser support” tricks as CSS hacks. In the past–for example, when working with IBM WebSphere Portal Server–I’ve relied on server-side rendering to deliver different CSS files to different browsers, but I wanted to minimize that, too, since I’m modifying an existing J2EE application that is as platform-agnostic as possible.

Then I discovered a hack that Microsoft built into Internet Explorer: conditional comments.

<!--[if IE 5]>
Welcome to Internet Explorer 5.
<![endif]-->

Now, there are a few things that are fundamentally wrong about conditional comments. The worst thing about them is that they violate the whole idea behind comments: comments should NOT contain executable code. Indeed, a common practice for disabling code in any language I’ve used is to “comment it out”: wrap your code in comments, and it becomes invisible to your compiler/interpreter. Wrapping code in comments and intending that code to execute is simply upside down.

Still, this seems to me a more acceptable hack than the other options. It doesn’t rely on browser bugs that, one would hope, the browser manufacturer will fix at some future point; it doesn’t result in unreadable CSS code; and it’s relatively unobtrusive, requiring only a head-scratching bit of commented code that executes in some browsers. Since it’s built on a “feature” of the browser most likely to cause you grief, this is a reasonable workaround that doesn’t adversely affect people who are able to use more standards-compliant browsers.

In my project, I deliver a full stylesheet to all browsers, and then conditionally include subsets that modify the CSS just for the browsers that misbehave.

For example, here’s an unordered list that is styled differently for IE6, IE7, and all other browsers, by using conditional comments:


  • Apple
  • Orange
  • Pear
  • Mango

If you’re using IE 7, you’ll see a blue background on the list items, and a yellow background on mouse hover; in IE 6, you’ll see the blue background but hovering won’t change the color (because IE 6 doesn’t support the “:hover” pseudo-class on non-anchor elements; more on that later . . .); and in any other browser, you’ll see an orange background with a blue hover. Here’s the code:

<style>
<!--
// all browsers (including IEx) see this CSS
ul.conditional-comments-example {
list-style:none;
color:#fff;
width:20em;
padding:3em;
border:1px dashed gray;
}
ul.conditional-comments-example li {
padding-left:2em;
background-color:#E17C1C;
}
ul.conditional-comments-example li:hover {
background-color:#1E4262;
}
-->
</style>
<!--[if IE 7]>
<style>
<!--
// only IE 7 sees this CSS
ul.conditional-comments-example li {
background-color:#016BAD;
}
ul.conditional-comments-example li:hover {
background-color:#ECE990;
color:#000;
}
-->
</style>
<![endif]-->
<!--[if IE 6]>
<style>
<!--
// only IE 6 sees this CSS
ul.conditional-comments-example li {
background-color:#4289AE;
}
ul.conditional-comments-example li:hover {
background-color:#EEEBD9;
color:#000;
}
-->
</style>
<![endif]-->

This is a trivial example, but useful for illustration purposes. In my own case, I found this approach helped me to fix some positioning problems (Firefox and Safari support “display:table” styles, IE doesn’t) and an annoying “jump” in mouse-over code that’s exhibited in IE.

This technique has some potential, if used sparingly and carefully; it would be nice if we didn’t have to use it, but it’s much cleaner than most of the other solutions I’ve seen.

Tags: , , , ,

For the last month, I’ve been deep in the bowels of a JavaScript project, which I’ve found to be equal parts fascinating and frustrating. Coming from a mostly Java background, I find the loosey-goosey aspects of JavaScript–weak typing, late binding, extremely forgiving syntax–particularly galling. In my LotusScript days, I was a big advocate of “option explicit” to keep me honest and avoid mistyping variable or function names; no such luck with JavaScript. There’s a lot to be said for the dynamism of JavaScript, of course, but it can come at the expense of the developer’s sanity.

Enter “lint” (or, at least, “lint-like”) tools for JavaScript. Though maybe not as nice as having Eclipse warn me about bad habits while writing Java, or Lotus Designer grumble when I save bad LotusScript, these tools apply a gimlet eye to my source code and goad me into cleaning up my act. A couple of them can be integrated into various JavaScript development tools, but since I’ve been working on this project in Notepad++ (which probably can be integrated with one or more, but I haven’t bothered to figure out how yet), I chose to use the on-line versions. Though there’s a lot of overlap, each tool–JSLint, JavaScript lint, and JSure–has its own strengths and quirks, so it’s not a bad idea to run your code through them all.

JSLint

WARNING: JSLint may hurt your feelings. Or at least it will if you feel that your JavaScript code is a reflection of your personal worth. If, instead, you see your code as a measure of your ability to wrestle with the beast that is JavaScript, then JSLint is a useful point of leverage in that struggle.

Of the three online JavaScript “lint” tools I’ve tried, JSLint is the strictest. As Douglas Crawford, author of JSLint (and the O’Reilly guide JavaScript: The Good Parts) states on the tool’s documentation page:

JavaScript is a sloppy language, but inside it there is an elegant, better language. JSLint helps you to program in that better language and to avoid most of the slop.

JSLint compares the code you paste into it against a set of coding standards, and lets you know where your code is lacking. It identifies unused and undeclared variables, provides a mechanism for identifying misspelled variable/attribute names, warns about comparison operators with type-casting side effects, and grumbles about misuse of “eval”-type statements that pass functions around as strings.

The interface is workmanlike: a no-frills page takes your input into a big text area, and lets you set a few switches (“Tolerate eval”; “Disallow bitwise operators”). The output appears between the input box and the switches, with each problem noted along with the line number and statement. I found that it worked best to fix a few of the issues, clear the JSLint input box, and repeat with the fixed code (especially since JSLint will only go so far before it gives up on you . . .)

JSLint output fragment

All in all, highly recommended for finding the nits hiding in your tangled JavaScript locks.

JavaScript Lint

JavaScript Lint has both online and downloadable flavors; the online version is similar to JSLint: you paste in your code, and you get a report that shows potential flaws. Unlike JSLint, the report shows you the errors in the context of the entire code block, with the errors flagged in red:

This is both good and bad: it’s nice to see things in context, but it’s difficult to navigate a large code block and scan for errors. A list of error links would make the output easier to use. JavaScript Lint also lacks the various switches and options that JSLint provides, though it also appears that its list of problems to check is less stringent than JSLint’s. There are a few switches available, like an “option explicit” implementation, but they can only be used by embedding comments directly into the source code. My prejudice is toward non-intrusive tools that don’t require me to change source code to use them, and that’s a mark against heavy usage of JavaScript Lint.

JavaScript Lint is built on the Firefox JavaScript engine. I would expect this to give it an edge on verifying that JavaScript code is standards-compliant, but may not make it so useful for checking code for non-compliant implementations (IE, I’m lookin’ at you . . .). All in all, though, JavaScript Lint is a useful and usable tool for flagging JavaScript errors.

JSure

A more recent entry into the JavaScript “lint” tools, JSure offers a few more input options than JSLint or JavaScript Lint: besides pasting the code into the online form, you can also pass a URL or upload a source file. And, like JSLint, it provides unobtrusive options for how code is reviewed.

JSure output fragment

JSure’s output format is somewhere between JSLint and JavaScript Lint: you see the errors and warnings in context, but only the functions with errors and warnings are displayed. This makes for a report that is both smaller than JavaScript Lint’s and more readable than JSLint’s. But, like JavaScript Lint, it’s not as comprehensive as the defensive developer might like; it lets a lot of loose practices slide that would cause JSLint to look askance at your code.

No doubt there are other “lint”-like tools for JavaScript available, perhaps even some that are easily incorporated into an IDE. My little visit to the world of JavaScript has taught me nothing if not that this territory is in flux: desperately in need of frameworks and tools now that JavaScript has become much more than an “optional” technology, the JavaScript community is providing world-class solutions that compare well to those that have been available for some time in the Java world. There’s still a bit of a “build it yourself” ethos that makes leveraging the advancements of others easy, but even that seems to be changing as JavaScript matures.

The purple lint guy is from Cheryl Capezzuti’s lint-sculpture project; send her the fluffy stuff from your dryer!

Tags: , ,

Switch to our mobile site