0

Is there any way to load a HTML file using plain Javascript and AJAX from an external page and show it ?

EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

1 Answers1

0

It's not so simple if it comes to external page because of the crossdomain issues, the simplest solution would be to use some backend i.e. Python, Ruby, PHP with Javascript (ajax)

  1. Javascript calls i.e. PHP file on same server
  2. PHP curl the external page and returns content
  3. Javascript after retrieving content process it and then adds to DOM i.e. with .innerHTML http://www.w3schools.com/jsref/prop_html_innerhtml.asp
maurycy
  • 8,455
  • 1
  • 27
  • 44
  • thank you for this, is there any code example somewhere? – EnexoOnoma Sep 02 '14 at 11:40
  • You can find php->curl documentation here http://php.net/manual/en/book.curl.php with some working example, from JS point of view it will be just a standard Ajax call and `.innerHTML` after data receiving – maurycy Sep 02 '14 at 11:47
  • Why suggest `curl` when `file_get_contents` is much easier to use? – SeinopSys Sep 02 '14 at 11:50
  • Well first of all file_get_contents is just a simple screwdriver when curl is a powerful tool, although it might be overkill if i think about it second time. And secondly I've met hostings service that were setting `allow_url_fopen` to false/0 for so called 'security' reasons so file_get_contents don't work there, but OP can try it, no harm in doing so ;) also cURL seems to be faster as well in most cases – maurycy Sep 02 '14 at 11:57