tags:

views:

60

answers:

3

Hi all!

Is it possible to load an external site's content using jQuery's Ajax rather than an iFrame?

This is what I am trying to achieve, but it would seem there might be some cross domain issues with this?

$('#result').load('http://www.google.com');

In flash you can put a cross-domain policy file in the root of the site allow certain sites to access the content of swf files or other files. Is this something that could be done using AJAX?

Thanks, James

+1  A: 

Is it possible to load an external site's content using jQuery's Ajax rather than an iFrame?

The Same Origin Policy usually prevents it. You can work around it using JSON-P to transport that data.

In flash you can put a cross-domain policy file in the root of the site allow certain sites to access the content of swf files or other files. Is this something that could be done using AJAX?

Not cross-browser, the standard is too new (and unfinished)

David Dorward
+2  A: 

It is possible, in part. You'll need to have your server act as a proxy:

$('#result').load('fetch.php?s=http://www.google.com');
// or something like that

As for having the client load a page cross-domain, it won't (shouldn't) be possible.

quantumSoup
The contents of fetch.php being: `echo file_get_contents($_GET['s']);` for example.
Ben Shelock
You'd probably want some validation on that input - imagine if somebody called that with s=/etc/password...
John Yeates
A: 

The Same Origin Policy does not allow to access a foreign domain via AJAX.

Anyway, you don't need so use AJAX to load a foreign site into an iframe. Just do

var $newframe = $('<iframe/>', {
    src:     'http://www.google.com',
    width:   '600px',
    height:  '400px'
}).appendTo(document.body);

Note: You can't access the contents of that iframe, because it's also following the same origin policy.

jAndy
is it possible to load an external site's content using jQuery's Ajax ***rather** than an iFrame?*
RobertPitt
no, not without browser privileges
jAndy