Skip to content

Instantly share code, notes, and snippets.

@courtenay
Created April 18, 2012 22:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save courtenay/2417083 to your computer and use it in GitHub Desktop.
Save courtenay/2417083 to your computer and use it in GitHub Desktop.
Tender: Sample jsonp autosuggest implementation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Test JSONP search api</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form action="#">
Search our knowledge base: <input type="text" name="q" >
</form>
<div ></div>
<div >
</div>
<script type="text/javascript" defer="true">
var results = $("#results")
var sending = 0, counter = 0;
$('#q').keyup(function(q){
sending = ++counter;
// if they're still typing don't double up requests
setTimeout(function(){
if (sending == counter) {
jsonp($("#q").val())
}
}, 1000);
})
function jsonp(value){
$.getJSON('https://help.tenderstage.com/help/search/autosuggest.json?callback=?',
{ q: value },
function(data){
var output = "<h2>" + data.length + " result(s)</h2><ul>";
for (var i=0; i<data.length; i++) {
output +=
"<li>Result " + i + ": " +
"<a href=\"" + data[i].link + "\">" + data[i].title + "</a>" +
data[i].formatted_body +
"</li>"
}
output += "</ul>"
results.html($(output))
})
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment