-
-
Save lgs/2419656 to your computer and use it in GitHub Desktop.
Tender: Sample jsonp autosuggest implementation.
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
<!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