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.

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.