Does anyone know if there are any ajax functions that would allow me to pull certain content from one page and display on another in a named <div>?

I currently have some code that pulls all the content and works very well. I am looking to enhance this to say only pull the first <div></div> content.

Thanks

If you are using Ajax, you can use the responseXML property which contains the document as an XmlDocument object that you can manipulate using DOM methods.

// Error handling omitted for brevity.
var req = new XMLHttpRequest();
req.open('GET', 'http://www.google.com/', true);
req.onreadystatechange = function (aEvt) {
  if (req.readyState == 4) {
     if(req.status == 200) {
       // make sure the XML is well formed otherwise 'xml' will be null
       var xml = req.responseXML;
       // grab hold of the first DIV reference
       var divObj = xml.getElementsByTagName("div")[0];
     }      
     else {
      dump("Error loading page\n");
     }
  }
};
req.send(null);

Javascript isn't able to make ajax requests between domains, so the above fix doesn't work. Instead, you have to rely on an intermediate PHP (or other server side language) script.

(As this is a high ranking result in google searches, I feel that it's necessary to reply here despite the age of the thread.)

Of course, as per the same origin policy it won't work if you are requesting resources which are not part of the same domain. It was a sample snippet posted for someone who probably wasn't aware of Ajax hence the 'google' domain.

BTW, given that this policy is enforced on the client side, an initiative has been taken by Firefox by introducing a new header, which hopefully might get implemented as a standard in like say 10 years? :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.