Eko
Eko

Reputation: 1557

HTML not formatted properly

I'm trying to get the HTML from another domain (Google for the example). I use this library, and I can get the HTML with it:

jQuery(document).ready(function($) {
    $.ajax({
        url: 'http://google.fr',
        type: 'GET',
        success: function(res) {            
            $html = $(res.responseText);
        }
    });
});

The string returned by res.responseText contains all the HTML, but when I do $(res.responseText), there isn't all the tags, for example I can't get the title by doing $(res.responseText).find("title"). I even tried the function $.parseHTML, I have the same result. Why is my HTML not parsed correctly?

Upvotes: 2

Views: 221

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Everything works as expected, but let's create a fake element:

$html = $("<div />", {html: res.responseText});
$html.find("title"); // This would work.

Upvotes: 1

Quentin Roger
Quentin Roger

Reputation: 6538

Can you try something like this ?

https://jsfiddle.net/omsmvksg/

jQuery(document).ready(function($) {
    $.ajax({
        url: 'https://google.fr',
        type: 'GET',
        dataType : 'html',
        success: function(res) {
            console.log(res.responseText);
            $html = $(res.responseText);
            console.log($html.find("._yKg").length);
        }
    });
});

It logs the good count of _yKg class in the html downloaded.

enter image description here

Upvotes: 1

Related Questions