sazr
sazr

Reputation: 25928

Obtain the html source/text of another webage through JQuery

Is it possible to:

The following different attempts don't work:

var html = $.get("http://simplyrecipes.com/recipes/braised_turkey_legs/").html();
var html = $.get("http://simplyrecipes.com/recipes/braised_turkey_legs/");
var html = $("http://simplyrecipes.com/recipes/braised_turkey_legs/").html();

Upvotes: 0

Views: 64

Answers (3)

whg
whg

Reputation: 894

Javascript has a same origin policy, this is what's holding you back.

Upvotes: 1

Tom
Tom

Reputation: 5201

jQuery's get function does not return the loaded data - rather, it calls a callback function and passes the data as a parameter.

This is from the documentation:

$.get('ajax/test.html', function(data) {
  $('.result').html(data);
  alert('Load was performed.');
});

P.S.: Note that in any case this will only work on the same domain, as cross-domain AJAX is normally not supported for security reasons.

Upvotes: 0

adeneo
adeneo

Reputation: 318212

Using Ajax, and get is just a shortcut for ajax, it's not really possible to get the source html from other domains, if that is what your trying to do? as ajax has a same origin policy for security reasons.

However going thru YQL it is possible to do is, read this to see how, or you could proxy with php or something else, with just regular get requests however it's not doable.

If the pages you are trying to get are on your domain, .load(); would probably be better.

Upvotes: 1

Related Questions