Last active
September 4, 2017 15:40
-
-
Save dejurin/601d9069794b5694656ef6121448cb01 to your computer and use it in GitHub Desktop.
Javascript Google Suggest Autocomplete
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
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