Widget:Book title/doc

< Widget:Book title

This widget emits JavaScript code that uses the Google Books API to retrieve and display a book title (and subtitle if one exists) for a given ISBN. The ISBN is supplied by preceding widget invocation with a suitable {{code| produced by Special:BookSources. That page removes punctuation from the supplied ISBN before generating, so the widget does not need to handle punctuated or other formatted ISBN numbers.

Book titles are retrieved asynchronously, and after the page has loaded.

Parameters
The widget take no parameters directly. Instead, it takes its input ISBN from the value field of the directly preceding {{code| {{#widget:Book title}} produces: {{#widget:Book title}}

As a convenience, the full invocation can be created with Template:Book title, which wraps calls to this widget with template code to generate the introductory {{code|. This is to ensure that they do not clash with any other JavaScript shipped by MediaWiki, either now or in future. The book title code is in.

Overview

 * On loading the page,  retrieves the target ISBN, and if it passes validation, it starts an asynchronous call to the Google Books API to retrieve book data. Data is requested in JSON format. Invalid ISBNs are indicated by (Invalid ISBN).


 * When the Google Books API call completes, it changes the text of the associated element to the book title, or (Title not found) if the API response does not contain a book title.

Multiple transclusion

 * The JavaScript code finds the requested ISBN by searching the DOM for elements with class bh-book-title. The value attribute contains the ISBN, and the text inside this element is where the book title is displayed.
 * Caller code should place the required element just ahead of the JavaScript source. On page load, the JavaScript finds all existing and matching  elements, and then starts a Google Books API call for the last one found.
 * Because elements appearing later on in the DOM have not yet been parsed, this means that the last one found is always the one written placed just ahead of this section of JavaScript. This indirect way of moving caller data into JavaScript works around MediaWiki's framework restrictions.

Portability

 * The code uses a relatively minimal set of JavaScript features. It tries to avoid using features that might not be present in some older browsers.