Rajasekar
Rajasekar

Reputation: 18938

Load a external URL into a div using jQuery

I want to load a whole site into a div. When I use

$(document).ready(function(){
        $('#result').load('http://www.yahoo.com');
    });

It's not working.

Upvotes: 3

Views: 6069

Answers (5)

Town
Town

Reputation: 14906

You can't do that unless the content you're loading comes from the same domain as the site you're loading it into, due to JavaScript's Same Origin Policy.

Your alternatives:

  • load the content into an iframe
  • pull the content server-side via an HTTP get, and the write it out to your page

Beware of licensing issues with the second option if you don't have permission to use the content, though!

Upvotes: 1

Jura Khrapunov
Jura Khrapunov

Reputation: 1024

Although I'm not sure about your use case of loading "whole" site into div - you are limited by "same domain" security policy, in order to make cross-domain AJAX calls you need to employ JSONP call http://api.jquery.com/jQuery.getJSON/

Upvotes: 1

Orbling
Orbling

Reputation: 20602

What do you mean "a whole site", if you mean a given page, then it'll probably require all manner of header included files, which are not suitable to go in to the body of your page.

You would need to use an IFRAME, just create the IFRAME element and set the source to the URL you want.

Upvotes: 1

Variant
Variant

Reputation: 17365

This is a cross-domain issue.
You can create a proxy on your server to fetch the data and you'll load it from your own domain.

Upvotes: 1

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

It will be a cross domain call to do using javascript. You can use iframe to load. Check this link for possible solutions.

Upvotes: 3

Related Questions