javascriptjsongetgoogle-search-apipage-title

Is there a way to get a website's full title from the Google Custom Search API?


I'm making a web application that uses the Google Search api, but my problem is that it only gets the first 10ish words from a website's title. Is there any way I could extend to get the whole title, or, a better option, get the contents of the web page and just parse the contents of the <title> tag? I'm using pure JavaScript. Thanks!


Solution

  • You will have to make a request to the URL, grab the HTML, and parse out the title from the HTML.

    function httpGet(theUrl)
    {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
        xmlHttp.send( null );
        return xmlHttp.responseText;
    }
    
    httpGet('http://some/url', function(response) {
        // parse the title tag here
    });