Read an HTML page from the server in a variableTag(s): Language


[N4 or better]
<HTML>
<HEAD>
<SCRIPT LANGUAGE= "JavaScript1.2">
function getPage() {
  // the file to be read
  pageURL = new
      java.net.URL
        ("http://www.yourserver.com/yoyrpage.html");

  // step 1, open the URL
  var openConnection = pageURL.openConnection;
  theConnection = openConnection()

  // step 2, connect to server
  var t=theConnection.connect
  t()

  // step 3, read the file using HTTP protocol
  var getContent = theConnection.getContent
  var theURLStream = getContent()

  // step 4, get an handle and fetch the content length
  var readStream = theURLStream.read
  var gcl = theConnection.getContentLength
  gcLen = gcl()

  // and finally, read into a variable
  theText =""
  for (i = 1; i <gcLen; i++) {
   theText += new java.lang.Character(readStream())
   }

  // for demonstration
  alert(theText)
}
</SCRIPT>
</HEAD>

<BODY>
<FORM>
<INPUT TYPE="button" VALUE="get page" onClick="getPage();">
</FORM>
</BODY>
</HTML>
After pressing the button, wait a few seconds while the page is read, then an Alert box containing the page content will appear, if the Alert box is bigger (probably) than the screen, just press ENTER to close it.

The above HowTo is for Netscape.

With IE, we can load the page in hidden frame and then get its content with

var sContent = document.all(0).innerHTML;
You can save / restore the body and not the whole document by using
document.all("content").innerHTML
where you have given the body an 'id' attribute of "content".

See also this related HowTo


blog comments powered by Disqus