0

This is not loading the website that I wanted.

$('#example').load("http://www.example.com");

http://www.jsfiddle.net/JFdVv/

Cœur
  • 37,241
  • 25
  • 195
  • 267
getaway
  • 8,792
  • 22
  • 64
  • 94

2 Answers2

5

You can't load content from a domain other than the one you're on unless it's JSONP (JSON with a function wrapper)...you can't load plain HTML like you're trying, it's blocked for security reasons by the same origin policy.


As an aside, the reason you get an error with example_ajax_request inline in the page is that by default jsfiddle puts your JavaScript code in a wrapper...you need to have functions like that directly in the page (global functions, not scoped to a ready handler), notice the first drop down up top...it needs to be "no wrap" (either one), instead of "onDomReady".

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • @getaway - You can't do this in JavaScript directly...you could proxy the content through the domain you're on, but no other option that that for fetching non-JSONP content. – Nick Craver Dec 02 '10 at 10:29
  • oh okay, how about if i wanted to load an iframe then, or if its a subdomain – getaway Dec 02 '10 at 10:29
  • @getaway - subdomain also is blocked, iframe would look something like this: http://www.jsfiddle.net/nick_craver/JFdVv/2/ – Nick Craver Dec 02 '10 at 10:31
  • oh okay thanks, i can mark you as the right answer when it lets me :)), can i also ask you know when i load the iframe, the browser is still reloading, is thier anyway to remove that, i would rather have a my own loading bar on the page. if you get what i mean, so it makes it look instant lol :)) – getaway Dec 02 '10 at 10:34
  • @getaway - you can add your own animations, but you can't remove the built-in loading indicators of the browser...they're there *somewhat* for security purposes as well, alerting the user something's happening. – Nick Craver Dec 02 '10 at 10:35
  • @getaway - I agree, iframes are always a messy approach...you'd be better off proxying the content as far as looks go, but it's not the best practice, since you're taking their content out of context. Something like a lightbox plugin to show the content works with a clear distinction though. – Nick Craver Dec 02 '10 at 10:45
0

If you really must load a page from different website, you can always use an <iframe> although this practice would be questionable to say the least.

Or, for a server-side solution, if you're using PHP, you can have a look at the PHP cURL library.

Valentin Flachsel
  • 10,795
  • 10
  • 44
  • 67
  • is using an iframe a bad practice? – getaway Dec 02 '10 at 10:36
  • [This question](http://stackoverflow.com/questions/362730/are-iframes-considered-bad-practice) should give you a much more insightful answer on that. However, when I said that I was referring to loading a full page from another domain, like in your example. – Valentin Flachsel Dec 02 '10 at 10:39