As I understand it, the JQuery load() function retrieves a document and can perform the quivelent of:
$('#somecontenttograb').html()
upon it. I would like to know how to do that… In essence, I want to use JQuery to asynchronously download a web page and then grab elements from it and insert them into my own document at different places.
I’m thinking the .ajax call is good here, but I’m having trouble understanding the returned object – I believe the argument passed to the success function could have it’s elements pulled out, but how?
Thanks,
Matt.
Having tried the code posted in the two answers below, I have tried this and while I can use an alert to show me the url being passed to the ajax() call and the success function gets called, if I alert the $(result).find(‘#content’) only null appears in the alert. If I put the url from the first alert into a browser, I get a valid page back. What could be going wrong?
Matt.
UPDATE
In response to the comments you’ve left on other answers, I just want to make sure I understand the situation clearly:
alert(...)-ing the result give you null, or some equivalent.If those things are true, then I’d try the following do some troubleshooting:
ajax(...)method, and handle failure by including some logging code to see what happened. You may be able to avoid this step if the page you’re on doesn’t need a visual response for a failed request and you’re using Firebug.Here’s the code I would use for this:
If you post the output of your Firebug logs, it should be easier to figure out the problem and find a solution for you. Firebug also logs
XMLHttpRequestsso you can see exactly what is being sent to and from the server, and Firebug will change the look of the request if it returns some kind of server error (which is how you could avoid #3 of the things I listed above).You could use the
ajax(...)method, but it would be easier to useget(...)with a callback, like this:responseDocwill be a jQuery object you can use to extract elements from and treat just like you would any other jQuery object. You can extract stuff from theresponseDocand add it into your main document any way you want, using the jQuery manipulation methods.If you need the extra functionality the
$.ajax(...)method provides, you would use the following code.