APIs for Librarians

Helping you to help yourself in helping your patrons. Or something.

Suggest Random Database


Description

This will display a link to a random database from your AZ list in LibGuides. You can narrow your results by subject if you like. Every time the page is loaded this code will show a different database.

You might wish to place this on a research guide or even in your discovery tool. FYI, you don’t need to include the text “Have you considered?” You can easily just use the database link itself.

Screenshot

Suggest Random Database screenshot

More details

Works with: APIs for Librarians: Springshare Auth Server

The API call, which is written in jQuery, sends a request to a proxy server that handles authorization for the API and then grabs the data and then returns it to our code. You’re welcome to use the Springshare Auth Server mentioned just above or roll your own.

The code takes a look at the returned list of databases and randomly selects one to display. You can easily limit the possible databases to specific subjects or it can be from your entire AZ list.

The Code

HTML

1
2
3
4
<div id="random-database-container">
    <h4 id="random-database-header">Have you considered?</h4>
    <div id="random-database"></div>
</div>

CSS

1
2
3
4
#random-database-container {font-family: 'Martel', serif;font-size: 1.2em;text-align: center;}
#random-database-header {margin-bottom: 0px;}
#random-database {margin: 10px 0;}
#random-database a {text-decoration: none;}

JavaScript/jQuery

Notes for implementation:
The code itself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var apis4librarians_randomDatabase = (function() {
var getRandomDatabase = function() {
$.getJSON(
"https://YOUR_SERVER_HERE.herokuapp.com/springshare/libguides/passthrough?what=/az?subject_ids=41905",
function(result) {
console.log(result);
var entry = result[Math.floor(Math.random() * result.length)];
var randomDatabase = '<a href="' + entry.url + '">' + entry.name + "</a>";
$("#random-database").html(randomDatabase);
}
);
};
getRandomDatabase();
})();