Link to home
Start Free TrialLog in
Avatar of MasonWolf
MasonWolfFlag for United States of America

asked on

How do I read an HTML page into a javascript string variable?

I have a dynamically created page with a very small amount of html on it. I can read it into a javascript through php to initialize it, but while the user is viewing the page, the dynamic link may need to change. Basically, I need a way to read the new page into a javascript string, and then from there I can use it for what I need it to do.

Is there a class or function that's supported by most browsers that will allow me to do this?

As an alternate solution, I could read in all the potential page iterations at the time the page is pulled from the server and save them to javascript array variables. But I'm hoping someone at ee knows a better way.
ASKER CERTIFIED SOLUTION
Avatar of DigitalTyrant
DigitalTyrant

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MasonWolf

ASKER

I tried to modify it and place it into a test script, but I don't know AJAX at all, so I think I'm doing something wrong.

Here's the test script.

<script type="text/javascript">
function GetXmlHttpObject()
{
  var objXMLHttp=null;
  if (window.XMLHttpRequest)
  {
    objXMLHttp=new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  return objXMLHttp;
}
function getHTML(querystring)
{
  var xmlHttp;
  var returnString;
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="https://www.experts-exchange.com/questions/22740977/How-do-I-read-an-HTML-page-into-a-javascript-string-variable.html";
  xmlHttp.onreadystatechange=function()
  {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
      //document.getElementById('htmlElement').innerHTML=xmlHttp.responseText; //sends HTML to an element
      returnString = xmlHttp.responseText;
    }
  };
  xmlHttp.open("GET",url+querystring,true);
  xmlHttp.send(null);
  return returnString;
}
document.write(getHTML('?cid=239'));
</script>

The error I received back was: "uncaught exception: Permission denied to call method XMLHttpRequest.open"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah! Ok, I gotcha. The script in question is on the same domain as the one I'm calling in the functional page, but I was using a different server to test. Thanks guys.
Your welcome!  So the EE URL was just for posting here.  And here I thought you had some script to get EE question info.  :)

I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
Avatar of DigitalTyrant
DigitalTyrant

The line:
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
is required if you want to use
xmlHttp.send(querystring);
Sorry, there was a typo in the original.