Skip to content

Instantly share code, notes, and snippets.

@mr-niels-christensen
Last active October 29, 2016 09:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr-niels-christensen/58f15e44c28d7820d712 to your computer and use it in GitHub Desktop.
Save mr-niels-christensen/58f15e44c28d7820d712 to your computer and use it in GitHub Desktop.
Semantic Web Recipes: Add a description using jquery and dbpedia lookup
<html lang="en">
<body>
<div class="item">
<b>Insert title here</b>
<div>Insert description here</div>
</div>
<div class="item">
<b>Insert title here</b>
<div>Insert description here</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function _set_html_from_dbpedia_description(selector, search_for) {
$.ajax({
url: "http://lookup.dbpedia.org/api/search/KeywordSearch",
data: {
"QueryString" : search_for,
"MaxHits" : 1,},
dataType: 'json',
success: function( response ) {
if ( response.results[0] ) {
$( selector ).html(response.results[0].description + " <i>[Source: Wikipedia]</i>");
};
},
timeout: 2500,
});
}
var data = [{"label" : "W3C", "desc" : "An organization"},
{"label" : "SEPA" } //No desc!
];
$( ".item" ).each( function(index, dom_elem) {
var item = data[index];
$ ( dom_elem ).find( "b" ).text( item.label );
if ( item.desc ) {
$ ( dom_elem ).find( "div" ).text( item.desc );
} else {
_set_html_from_dbpedia_description( $ ( dom_elem ).find( "div" ), item.label );
}
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment