April 3, 2013
how can i obtain an entire element from an external page?
Cola89’s Questions:
how can i get a
<table class="precios">
from another url (www.example.com) in HTML format?? because with DOM i can obtain the table but in an array mode.
Thank you everyone for helping
Assuming you have no cross-domain issues, you can use .load()
for that:
$container.load('http://www.example.com/path/to/page table.precios');
Whereby $container
is a jQuery object where you want to “save” the table into.
Within PHP you would solve it this way:
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile('http://www.example.com/path/to/page.html');
libxml_clear_errors();
$xp = new DOMXPath($doc);
$table = $xp->query('//table[@class="precios"]')->item(0);
echo $doc->saveHTML($table);
Make a AJAX request to that page and use .find()
to get the element
$.ajax( {
url: myUrl,
success: function(html) {
table = $(html).find(".precious");
}
});