2022.2

Table Of Contents
IfthereturnedcontentisJSONdata,thatdatahastobewrappedinHTMLbeforeinsertingitintothe
template.Thisisdemonstratedintheexamplebelow.
Tip: InstallthePostmanapplicationtopreviewJSONreturnedbyanendpoint.
Tip: ToloadaJavaScriptfile(.js)orastylesheet(.css)youcanuseloadtext().See"loadtext
()"onpage731.
Inserting content in the template
Toinsertthecontentaftertheselectedelement,useresults.after().Toreplacetheelementwith
thenewcontent,useresults.html()orresults.replaceWith().
Example: recent posts
ThefollowingscriptloadsfivepostsfromMozilla'sblogandinsertstheirtitlesaslinksinatemplate.
Mozilla'sblogisaWordPresswebsite.SincetheWordPressRESTAPIusesJSONastheresponse
format,theloadjson()functionhastobeusedandthereceivedcontenthastobewrappedinHTML.
Ifthescript'sselectorwash1(aleveloneheading),theretrievedcontentwouldbeinsertedaftereach
leveloneheading.
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WordPressRESTAPIdeveloperendpointreference.
Tip: MoreexamplesofhowtouseanAPItoloadexternalcontentaregivenintheseHow-to's:
l
UsingtheGoogleMapsAPI
l
UsingtheOpenWeatherMapAPI
Using scripts in Dynamic Tables
IfthecontentsorstyleofcellsorrowsinaDynamicTableneedtobepersonalizedbasedonthevalue
ofadatafield,youwillneedascript.
Doyouwanttostyleorchangetheoutputofanexpression,basedonthevalueofadatafield?Thena
customHelpercancomeinhandy.AHelperisafunctionthatcanbeusedinanexpression.Howtocre-
ateacustomHelperisdescribedinthetopic:"CreatingcustomHelpers"onpage336.Aftercreatinga
HelperyoucantypethatHelper'snameinanyexpression.
Page 394