views:

10

answers:

1

When I try to get the contents of a htm file into a div using a xmlhttprequest object in Firefox it includes everything, but in IE it only includes the contents of the body tag. In other words it ignores all the styling (in the head tag) of the page, rendering it ugly.

Is it possible to get the full page when using xmlhttprequest in internet explorer?

edit:

document.getElementById('divtoreceivetheresponse').innerHTML = xmlHTTP.responseText

This line in FF gets the page contents including the <head></head> section.

In IE it just gets the contents inside the <body></body> section.

A: 

I got an answer from elsewhere. Basically it does include all the page (not just the body) but IE chooses not to render it (probably the correct behavour)

I therefore worked out some code to extract the css, place it in the head, and place the body stuff in the target div. So both html and css from the external page would be got.

<html><head>
<script type="text/javascript" language="javascript">
function include(lyr,url)
{
   if (document.all)
   {
      try {
      var xml = new ActiveXObject("Microsoft.XMLHTTP");
      xml.Open( "GET", url, false );
      xml.Send()

       }
      catch (e) {
      var xml = new ActiveXObject("MSXML2.XMLHTTP.4.0");
      xml.Open( "GET", url, false );
      xml.Send()
      }
   }
   else
   {
            var xml=new XMLHttpRequest();
            xml.open("GET",url,false);
            xml.send(null);
   }

   text = xml.responseText;
   text = text.replace("<html>","");
   text = text.replace("</html>","");
   text = text.replace("<head>","");
   text = text.replace("</head>","");
   text = text.replace("<body>","");
   text = text.replace("</body>","");
   splittext = text.split("<style type=\"text/css\">");
   splittext = splittext[1].split("</style>");
   css = splittext[0];
   everythingelse = splittext[1];

   addCss(css);
   document.getElementById(lyr).innerHTML=everythingelse;   

}

function addCss(cssCode) {
var styleElement = document.createElement("style");
  styleElement.type = "text/css";
  if (styleElement.styleSheet) {
    styleElement.styleSheet.cssText = cssCode;
  } else {
    styleElement.appendChild(document.createTextNode(cssCode));
  }
  document.getElementsByTagName("head")[0].appendChild(styleElement);
}


</script>
</head>
<body onload="include('adiv','test.htm')">
<div id="adiv">sdfgboui hsdguhwruh o ikuy </div>
</body>
</html>

The code is far from perfect, but it does the job and I will probably improve the code bit by bit now that I know it works

MrVimes