Zotero-JavaScript-Search-Client

DOI

Example HTML, CSS, and JavaScript for searching for items within a public Zotero user or group library

See the Repo

Live demos:

Motivation

To help users discover your publications, you can add them to an online database like Zotero and then present a search interface to that database on your website. Modules for doing so have been written for WordPress and Drupal, but I hadn’t seen one for static HTML sites, so I wrote this example to test how feasible a static HTML Zotero client would be to implement.

Usage

Open the zotero.html file in your browser and enter a search term like coastal. Click the search button and see results as formatted by Zotero to the page.

You can click Search with no terms specified to show the entire catalog, which is the default behavior when you load the page.

Zotero limits the number of results returned in a single request, so the page supports pagination to get additional results.

To use this for your own project, you will need a free Zotero account into which you include your publications, and obviously you’ll need to adapt the HTML, CSS, etc., for your particular website.

Customization

The code finds elements via their HTML id attribute values, so in general you’ll want to start with one of the example HTML documents and adapt it to your needs.

To change parameters such as how many search results to show at a time, see the ZOTERO_CONFIG variable in zotero.js. See comments in the code for a brief explanation and example values, or the text below for additional details. I recommend setting your parameters in your HTML file. That way, you can overwrite zotero.js when enhancements are made to this repository, without having to edit the parameters in zotero.js for your usage. For example, just before the closing </body> tag in your HTML, add a script tag like this one:

<script>
   ZOTERO_CONFIG["zotId"] = "2055673";
   ZOTERO_CONFIG["collectionKey"] = "";
   ZOTERO_CONFIG["filterTags"] = "&tag=LTER-BLE";
</script>

You can search a user library, a group, or a collection within a user library or group.

Results Table

Results are displayed in a table with up to four columns: Year, Citation, Item Type, and a special column (we’ll refer to it as ShowTags) showing the presence of particular tags.

The allowable values for the style parameter are filenames from the Zotero Style Repository, without the .csl extension. Find the style you want on that page, and then hover you mouse over the style name and click Source, and then read the id for the style. For example, the filename for Analytica Chimica Acta: X is actually analytica-chimica-acta-x. You can also host your own style file (*.csl) on a public facing Web server and provide the full URL to the file in ZOTERO_CONFIG’s style parameter, as in:

ZOTERO_CONFIG["style"] = "https://mysite.org/mystyle.csl";

You can supply links to datasets that the publication references by placing each dataset’s DOI (with https://doi.org/ in front of it) on its own line in the Extra field in Zotero, as in:

https://doi.org/10.18739/A2GX44T8M
https://doi.org/10.18739/A2MP4VN1H

Result Counts and Pagination

You can provide HTML id attribute values for elements that will contain result counts, pagination links, and so on. In general, if you do not want to show one of these elements, then provide an empty string, “”, as the identifier. If the code does not find a matching element, it will skip it.

Advanced Search Controls

The Zotero API has very limited advanced search functionality, though the developers say they are planning to add more features. Currently you can only filter by item type. Thus, zotero.html includes a collapsible Advanced section in which the user can choose to filter by item type. See the HTML if you’re curious, but for now I suggest leaving the advanced section out until more advanced functionality is supported.

Sorting

The Zotero API supports sorting results. If you want to allow the user to sort results, you must include an HTML select control with the identifier visibleSort and with options matching the allowable Zotero sort values. See the section on sorting and pagination parameters in the Zotero API documentation for allowable values. See zotero.html for an example of what the select element should look like. This element should be contained within a div element, which is used to hide the control if no results were returned from the search.

If you do not want to allow the user to change the sorting option, then leave these HTML elements out of your document.