views:

824

answers:

5

Hello Friends,

Lets assume I have my browser load an Iframe with <iframe src="test.html">

Can I, using ajax, load the content of test.html into a div in the main html page?

This idea is my solution for that fact that I'm actually trying to overcome the limitation with making ajax submits to remote hosts. The plan is to generate the dynamic page with 0 sized iframe which makes report request to remote host. Then, after the page (& iframe content) loads I will copy the iframe content into a div using JS.

Tips are appreciated,

Thank you, Maxim.

+1  A: 

Can I, using ajax, load the content of test.html into a div in the main html page?

Yes (since your example has a relative URI and is on the same host) …

This idea is my solution for that fact that I'm actually trying to overcome the limitation with making ajax submits to remote hosts.

… and no. You still can't read data from remote hosts.

David Dorward
+2  A: 

No, you can't.

When you load a page from a different domain into the iframe, it becomes unreachable. You can no longer access the contents of the iframe, as it comes from a different domain.

The only thing that I know of that you can reliably load from a different domain is a script, which JSONP uses.

Guffa
bummer... I was sure that because I can kung foo the iframe all my ajax issues are solved.Thanks for the explanation.
Maxim Veksler
@Maxim: Funny, I actually thought the same earlier today, for a few seconds before realising that I wouldn't be able reach the data that I had loaded... :)
Guffa
A: 

... you may, however, design an AJAX request to local host and retrieve information from the remote server (as described here).

St.Woland
A: 

I'm sure someone will correct me if I'm wrong, but I believe that scripting across domain boundaries is restricted. Have you tried it? Here's a function that may help out.

function insertDivFromFrame(divname, framename) {
    var frame = document.getElementById(framename);
    var d = frame.contentWindow || frame.contentDocument;
    if (oDoc.document) {d = d.document;}
    document.getElementById('yourdiv').innerHTML = d.body.innerHTML;
}

I'm not sure this code works... see http://xkr.us/articles/dom/iframe-document/ for more help on this.

Emtucifor
A: 

If you write a php/perl/etc. script to output the contents of a document from another domain, it'll give you access to the contents as the resulting page would be considered by javascript to belong to your domain. If you're not familiar with any server-side scripting languages, I'm sure you'd be able to find a script that'll do this for you by doing a simple google search.

Best of luck.

James
This is what we will probably end up doing using Apache HttpClient as simple forward proxy.I'm still having doubts regarding a better/best proxy solution: webapp software proxy vs. Servlet Container Proxy vs. mod_proxy ?
Maxim Veksler