user711250
user711250

Reputation: 25

AJAX reload with specific DIV

I cannot find an answer on the following issue. I have managed to reload a specific part of my page (page A) by using AJAX (through document.getElementById). I would like to know if there is a way to choose which part of the document (page B) will be used to reload the content of page A. In other words, pick a specific DIV from a 2nd page (same domain) and use it to refresh the contents of my page. I have seen in other threads that it is not possible to do it with pages that aren't from the same domain. But in my case I will use a page from the same domain. Any ideas? Thanks.

Upvotes: 1

Views: 1056

Answers (1)

Pavel Koryagin
Pavel Koryagin

Reputation: 1481

If you have page A loaded and your scripts run there, so this will do:

var ifr = document.createElement('iframe');
ifr.src = 'page B URL';
ifr.style.position = 'absolute';
ifr.style.left = '-1000px';
ifr.onload = function() {
  document.getElementById('page A div id').innerHTML =
    ifr.contentDocument.getElementById('page B div id');
}
document.getElementsByTagName('body')[0].appendChild(ifr);

Note: page B loads and runs completely. If you need the only div to be passed through the Internet connection, you need to implement server side logic.

Upvotes: 1

Related Questions