0

I'd like to somehow get the page source of an external URL, and with that, be able to get the contents of an h1 element.

For example, this is the logic:

 var url = "http://example.com";

 var src = // page src of url 

 // instead of document, it would use the variable src
 var headerText = document.getElementsByTagName("h1")[0].innerHTML;
 alert(headerText);

I know I can get stuff with curl/php, but have heard larger sites will cause server strain. I'd prefer to keep this as efficient as possible. Not really sure where to start.

O P
  • 2,327
  • 10
  • 40
  • 73

1 Answers1

0

This cannot be done, by policy.

In Web pages, JavaScript (and client-side scripts in general) aren't allowed to access raw external resources from a different domain, in general.

The closest you're going to get is using AJAX (jQuery helps) to access a resource, but you (in general) need to be requesting a URL under the same domain.

Chris
  • 26,544
  • 5
  • 58
  • 71