Why cant I load an external resource from jQuery load method?

Jquery uses an ajax (XMLHttpRequest) request to load the data, but the browser allows this for resources on the same domain. (The answers above mention the Same origin policy). That’s why it works with Temp.htm, but not www.google.com.

  • One way to get around this is to create a server script that will load the page for you – basically a proxy. Then you call

    $('#g').load("load.php?url=google.com")
    
  • The other solution is to use iframes for communication – I found this library, that seems to be what you need: jquery-crossframe

  • A third options is JSONP but that would not work it your case.

My opinion – go for the first option with a server-side proxy.


Why is there a same origin policy?

Imagine that you are checking some stuff on your ebay account. Then in another tab you open my site, where I have a script that makes a series of requests to ebay (you are still logged in) and bids you for an Audi A8 without you even noticing. Annoying… If it was your bank it can directly steal money from you.

The irony is that despite the same origin policy, the above attack is still possible.

Leave a Comment