Last active
October 29, 2016 09:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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