2021.2

Table Of Contents
Inserting content in the template
To insert the content after the selected element, use results.after(). To replace the element
with the new content, use results.html() or results.replaceWith().
Example: recent posts
The following script loads five posts from Mozilla's blog and inserts their titles as 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 and the received content has to
be wrapped in HTML.
If 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 an API to load external content are given in these How-to's:
l Using the Google Maps API
l Using the OpenWeatherMap API
Using scripts in Dynamic Tables
In most cases you don't need a script to change the content or style of rows or cells in a
"Dynamic Table" on page825.
Page 909