简体   繁体   中英

Google auto complete on textbox?

I want to show all google search auto completes in a textbox. After searching google i found " http://google.com/complete/search?output=toolbar&q=test " link that is return a xml data contains ten google search suggets. My jQuery code for showing this xml values is :

$(window).ready(function(){
    $( "#textbox" ).keypress = showSuggest();
});
function showSuggest() {
    $.ajax({
        type: "GET",
        url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val(),
        dataType: "xml",
        var data = [];
        success: function(xml) {
            $(xml).find('CompleteSuggestion').each(function(){
            data.push($(this).find('suggestion')[0].attr('data'));
        });
        }
    });
    $( "#textbox" ).autocomplete({
        source: data
    });
}

jquery-1.9.1.js and jquery-ui-1.10.3 was imported but this code not working. sorry for my bad english. thanks.

UPDATE

thanks to everyone. i edit my code and remove xml reading part and replace

url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val()
$("#textbox").autocomplete({
   source: data
});

with this :

$( "#textbox" ).autocomplete({
source: "http://suggestqueries.google.com/complete/search?client=firefox&q=" + $( "#textbox" ).val()
});

now on typing text only show progress image left side of textbox and still not showing suggets. please help.

NEW UPDATE

I write new code with firefox DOMParser but still not working.

    $("#textbox").keypress(function() {
    var data = [];
    var parser = new DOMParser();
    var xml = parser.parseFromString("http://google.com/complete/search?output=toolbar&q=" + $("#new-tag-post_tag").val(), "application/xml");
    xml.domain = "google.com";
    suggests = getElementsByTagName("CompleteSuggestion");
    for (var i = 0; i < suggests.length; i++) {
        data.push(suggests[i]);
    }
    $( "#textbox" ).autocomplete({
        source: data
    });
}
$(document).ready(function(){
    $( "#textbox" ).keypress(showSuggest);
});                

function showSuggest() {

    // Declare the variable here, not inside the $.ajax code
    var data = [];

    $.ajax({
        type: "GET",
        url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val(),
        dataType: "xml",            
        success: function (xml) {
            $(xml).find('CompleteSuggestion').each(function () {
                data.push($(this).find('suggestion')[0].attr('data'));                
                 $("#textbox").autocomplete({
                   source: data
                 });
            });


        }
    });
}

your javascript syntax is wrong. This could work:

$(document).ready(function() {

    $("#textbox").on("keypress", showSuggest);
});
function showSuggest() {
    var data = [];
    $.ajax({
        type: "GET",
        url: "http://google.com/complete/search?output=toolbar&q=" + $("#textbox").val(),
        dataType: "xml",
        success: function(xml) {
            console.log(xml);
            $(xml).find('CompleteSuggestion').each(function(){
              data.push($(this).find('suggestion')[0].attr('data'));
            });
            $( "#textbox" ).autocomplete({ source: data});
        }
    });
}

anyway I recommend you to learn JavaScript before you start programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM