Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrive Web Page via Javascript

 
Kevin Cornwell
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should be simple but I'm having trouble...

How do I get a web page with javascript and assign the entire page contents to a javascript variable?

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the content of the current page, then all you need to do is: var content = document.body.innerHTML;

If you want another html page, then the only way you can do it with JavaScript is if you are grabbing a page on the same domain and that your browser supports AJAX or iframes.

Eric
 
Kevin Cornwell
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Pascarello:

If you want another html page, then the only way you can do it with JavaScript is if you are grabbing a page on the same domain and that your browser supports AJAX or iframes.

Eric


use a hidden iframe or something?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var content = parent.iframeName.document.body.innerHTML;
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var content = document.body.innerHTML;


Is innerHTML supported on all the major browsers these days? I think it used to be an IE invention.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is supported by all of the major browsers eventhough it is not part of the W3C spec. innerText is not supported by all the browsers.

Eric
 
Kevin Cornwell
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic