1.8

Table Of Contents
Example: recent posts
The following script loads five posts from Mozilla's blog and inserts their titles as a list of links in
a template. Mozilla's blog is a WordPress website. Since the WordPress REST API uses JSON
as the response format, the loadjson() function has to be used.
f the script's selector was h1 (a level one heading), the retrieved content would be inserted after
each level one heading.
var postsObj = loadjson('https://blog.mozilla.org/wp-
json/wp/v2/posts?per_page=5');
var html = '';
html = '<ul>';
for (var idx in postsObj) {
html += '<li><a href="' + postsObj[idx].link + '">' + postsObj
[idx].title.rendered + '</a></li>';
}
html += '</ul>';
results.after(html);
See WordPress REST API developer endpoint reference.
Tip
More examples of how to use a RESTful API to load external content are given in these How-to's:
l Using the Google Maps API
l Using the OpenWeatherMap API
Control Scripts
When output is generated from a template, Control Scripts run before all other scripts, when a
record is merged with a context. They determine how different sections of the context are
handled. They can, for example, make the page numbering continue over all Print sections,
split Email attachments, or omit Print sections from the output.
Some knowledge of JavaScript is needed to edit Control Scripts, just as for any other self-made
scripts, because there is no Control Script Wizard; see "Writing your own scripts" on page624.
Page 645