Gabriel Scavassa
Gabriel Scavassa

Reputation: 594

Empty return from JQuery .contents()

I'm trying to get the HTML using:

$(document).ready(function () {
    var html = $("html").contents();
});

When I type "html" in chrome console I get "[]" what I think is the html variable is not loaded.

Doing the same thing in other web site, when I type "html" in chrome console i get

[<head>​…​</head>​ ,#text,<body class=​"page_color default_font ext-webkit ext-chrome" id=​"ext-gen3" style>​…​</body>​]

So, why there is that difference? In both application I have the Jquery.

Upvotes: 1

Views: 91

Answers (2)

marteljn
marteljn

Reputation: 6516

Since you are declaring html in a function, it is not available at the global scope for you to access via the console.

If you need to see it in the console you could declare html as global.

$(document).ready(function () {
    window.html = $("html").contents();
});

Upvotes: 1

Anton
Anton

Reputation: 32581

You have to define it globally like this

var html;
$(document).ready(function () {
    html = $("html").contents();
});

Upvotes: 3

Related Questions