Questions | Login

Top / Javascript

Javascript Question / getting elements from external page?

demo 22 Dec 2008 03:59pm
I'm trying to add some javascript to a page that will pull in data from another page on the web (not my page either). I'm trying to create a screen wiper that can look at a page that has dynamic content, summarize it, and present that in a different format so <iframe> is not an option.

My question is:
How can I get the page elements from an external document?

I have tried
document.Url="http://someexternaldoc.html";
but the document element still points to my own page.

Answers

demo 22 Dec 2008 08:49pm
Read this twice - it can't be done with JavaScript alone.

The browser security model precludes references into the document context of any page (URL) imported from outside the domain of origin of the page sending the request. It will not matter whether you include the document in an iframe (or frame) or get it via an XMLHttpRequest - if the URL is not in your domain, you can't touch (not even to read) the source, elements, or attributes of that external document.

The ONLY solution relies on server-side proxy of the request:

- your page makes a request to proxy service in your domain
- proxy service makes actual request, i.e., gets the URL
- proxy service receives response from the external domain
- proxy returns response to your page

This approach "fakes out" the browser, since all of the request/response aspects appear from the browser's perspective to be within the same domain of origin as the page making the request(s). This is the ONLY approach that will succeed under the current browser security model...all browsers.

Since the proxy service is in your domain, and its response is from within that domain, you can then do whatever you wish with the contents returned by the proxy.

Proxy services can be implemented in any server-side technology. I've written them in Java. My current crew uses PHP for to provide such services.

Related

Submit Answer

You must be logged in to post an answer. Please Login or Register.

Powered by phpMyAnswers.