0

I'd like to load the contents of a page in javascript without actually opening it. I want to use it so I can load a page and scan some of its elements to see if it should actually be opened in a new window/tab before doing the opening itself.

How can I do that?

TravisG
  • 2,373
  • 2
  • 30
  • 47
  • 1
    Does it follow the [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy)? – zzzzBov Dec 14 '11 at 18:33
  • Well, I'm very much a beginner in web development, and I have a very simple idea for a cool little site. I'm open to using something completely different than javascript if it isn't all too complicated. I'd rather have it running in a couple of minutes than learning about things I will never need again (I usually never develop web stuff and I probably never will again). edit: @zzzzBov, I basically just need to check the site that I want to open for content that's like "404 - not found", so I'm not sure if it follows the policy. The sites are always external – TravisG Dec 14 '11 at 18:35
  • 1
    @heishe: If you want to know if the page is not found, all you need to do is a `HEAD` request. However, you can still only do this server-side if the pages are not on the same domain as the main webpage. – mellamokb Dec 14 '11 at 18:39
  • A little update: Thanks for the replies so far, but I'm guessing it can be done a little bit easier. All I need to do is basically: var wnd = window.open(url), then somehow keep the window from opening until i want to open it manually, and until that time search through certain elements via window.document.getElementByID() – TravisG Dec 14 '11 at 19:08

2 Answers2

2

You can do this using the XMLHttpRequest object.

This is subject, however, to cross-domain scripting limitations.

Read more about it:

TreyE
  • 2,649
  • 22
  • 24
0

Load it in an Ajax call.

With jQuery:

$.ajax({
  url: 'http://your_page_url',
  success: function( data ) {
    // process data (which is actually your page contents)
  }
});
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • When I try this, the debugger says: XMLHttpRequest cannot load URLHERE. Origin null is not allowed by Access-Control-Allow-Origin. How do I fix this? – TravisG Dec 14 '11 at 21:06
  • what is your URL? this error is discussed [here](http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-file) and [here](http://stackoverflow.com/questions/5224017/origin-null-is-not-allowed-by-access-control-allow-origin-in-chrome-why) – Oleg Mikheev Dec 15 '11 at 05:44
  • Worth noting that this requires jQuery. – TreyE Jan 10 '12 at 12:50
  • That's why _With jQuery_ is in the answer :) – Oleg Mikheev Jan 12 '12 at 11:14