Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript Google Suggest Autocomplete
function getQueryGoogle(obj, callback) {
var id = "i" + Math.random().toString(36).slice(2);
getQueryGoogle[id] = function(data) {
callback(obj, data);
delete getQueryGoogle[id];
};
// Google clients1
loadScript("//clients1.google.com/complete/search?client=firefox&q=" + encodeURIComponent(obj.value) + "&callback=getQueryGoogle." + id);
// Yahoo
/*
loadScript("//query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22http%3A%2F%2Fsuggestqueries.google.com%2Fcomplete%2Fsearch%3Fclient%3Dfirefox%26q%3D" +
encodeURIComponent(obj.value) +
"%22%20&format=json&callback=getQueryGoogle." + id);
*/
};
function loadScript(setUrl, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
if(script.readyState) {
script.onreadystatechange = function() {
if(script.readyState === "loaded" || script.readyState === "complete") {
script.onreadystatechange = null;
//callback();
}
}
} else script.onload = function() {
//callback();
};
script.src = setUrl;
document.getElementsByTagName("head")[0].appendChild(script)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment