Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get html of external url in jquery

Tags:

html

jquery

url

How can i get external URL's HTML using jquery?

like image 886
hotcoder Avatar asked Oct 01 '10 08:10

hotcoder


Video Answer


1 Answers

The short answer is you can't, because AJAX requests are limited to the same (sub)domain and port by the Same Origin Policy.

The same restrictions apply to iframe elements: You can't create an iframe pointing to the external page, and grab its HTML from there.

The usual way is to use a server-side script (written e.g. in PHP) that serves as a proxy: It fetches the external site's content and serves it back to JavaScript. It will have to run on the same domain as the page.

Obviously, using this solution, relative references to URLs, images, stylesheets and such (e.g. ../images/image.gif) will no longer work, as they areout of context on your page. Whether that is a problem in your situation is impossible to tell. One workaround for that may be using a <base> tag.

like image 186
Pekka Avatar answered Oct 27 '22 14:10

Pekka