开发者

Load a external URL into a div using jQuery

开发者 https://www.devze.com 2023-03-15 08:52 出处:网络
I want to load a whole site into a div. When I u开发者_开发问答se $(document).ready(function(){

I want to load a whole site into a div. When I u开发者_开发问答se

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

It's not working.


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


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.


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.


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/


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消