Skip to content

Instantly share code, notes, and snippets.

@scripting
Last active November 17, 2016 17:40
Show Gist options
  • Save scripting/bd61650fce05dd3e1357 to your computer and use it in GitHub Desktop.
Save scripting/bd61650fce05dd3e1357 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Wordnik demo from JavaScript</title>
<script src="http://static.smallpicture.com/bootstrap/js/jquery-1.9.1.min.js"></script>
<script>
var baseUrl = "http://api.wordnik.com/v4/word.json/";
var apiKey = "a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5"; //demo key from developer.wordnik.com
function getSynonyms (theWord, callback) {
var url = baseUrl + theWord + "/relatedWords?useCanonical=true&relationshipTypes=synonym&limitPerRelationshipType=100&api_key=" + apiKey;
var jxhr = $.ajax ({
url: url,
dataType: "text" ,
timeout: 30000
})
.success (function (data, status) {
var array = JSON.parse (data);
console.log (data);
callback (array [0].words)
})
.error (function (status) {
console.log ("getSynonyms: url == " + url + ", error == " + JSON.stringify (status, undefined, 4));
});
}
</script>
<style>
.divPage {
width: 50%;
margin-left: auto;
margin-right: auto;
margin-top: 100px;
border: 1px solid silver;
background-color: whitesmoke;
padding-left: 15px;
}
</style>
</head>
<body>
<div class="divPage">
<p>This is a bit of sample code that accesses the <a href="http://developer.wordnik.com/docs.html">WordNik AP</a>I from JavaScript in the browser.</p>
<p>Here are the synonyms for "happy":</p>
<div id="idSynonymList"></div>
<p>For more info see this <a href="http://scripting.com/2014/06/28/usingWordnikViaJsInTheBrowser.html">blog post</a> on scripting.com.</p>
</div>
<script>
getSynonyms ("happy", function (words) {
var s = "";
for (var i = 0; i < words.length; i++) {
s += "<li>" + words [i] + "</li>"
}
$("#idSynonymList").html ("<ul>" + s + "</ul>");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment