Currently testing in: WinVista / IE7
I have been working on getting xmlhttprequest going for weeks now. I have finally gotten a semi-working script going. Pulling up text or xml files works great and populates into the webpage; however, executing javascript has a few problems:
1. Opens on a blank page, but not a new page. It appears to compeltely overwrite the original page. I want it to update the existing page by loading within a <div> element.
2. I cannot seem to get functions to work. It seems as if the script is not opening in the global space despite using the 'window' element.
3. What format should the external javascript file that I am attempting to load be in? I think that I have figured out not to include the '<!--' and '//-->' tags normally included in an external javascript file.
4. I have no idea how to tell when the browser is using JScript or JavaScript (friggin' Microsoft :-D) and how the two may respond differently.
I have been reading a lot on the global issues, but none of the suggested fixes has worked for me. One thing that I've learned is that in IE7, all three of these methods of initiating an external javascript file work: eval(), execScript(), and setTimeout().
Here is the code for your viewing pleasure (I break my code out for ease of reading; so I apologize if it seems longer than normal):
[code=javascript]
<!--
var xmlhttp;
var fileType;
function loadData(url)
{
xmlhttp = null;
fileType = url;
// code for native xmlhttp
if (window.XMLHttp Request)
{
xmlhttp = new XMLHttpRequest( );
}
// code for old IE
else if (window.ActiveX Object)
{
xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
}
if (xmlhttp != null)
{
xmlhttp.open("G ET", url, false);
xmlhttp.onready statechange = processData;
xmlhttp.send(nu ll);
}
else
{
alert("Please upgrade to a browser that supports XMLHTTP.");
}
}
function processData()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyS tate == 4)
{
// if "OK"
if (xmlhttp.status == 200)
{
// if text document
if (fileType.lastI ndexOf("txt") != -1)
{
document.getEle mentById('conte nt').innerHTML = xmlhttp.respons eText;
}
// if xml document
if (fileType.lastI ndexOf("xml") != -1)
{
// xml parse code here for xmlhttp.respons eXML
}
// if javascript file
if (fileType.lastI ndexOf("js") != -1)
{
// if MSIE
if (window.execScr ipt)
{
window.execScri pt(xmlhttp.resp onseText, "javascript ");
return null;
}
// if others
else if (window.eval)
{
window.eval(xml http.responseTe xt);
}
// if Safari
else
{
window.setTimeo ut(xmlhttp.resp onseText, 0);
}
}
}
else
{
alert("Problem retrieving data:\n" + xmlhttp.status + "\n" + xmlhttp.statusT ext);
}
}
}
//-->
[/code]
Thanks in advance for your help.
I have been working on getting xmlhttprequest going for weeks now. I have finally gotten a semi-working script going. Pulling up text or xml files works great and populates into the webpage; however, executing javascript has a few problems:
1. Opens on a blank page, but not a new page. It appears to compeltely overwrite the original page. I want it to update the existing page by loading within a <div> element.
2. I cannot seem to get functions to work. It seems as if the script is not opening in the global space despite using the 'window' element.
3. What format should the external javascript file that I am attempting to load be in? I think that I have figured out not to include the '<!--' and '//-->' tags normally included in an external javascript file.
4. I have no idea how to tell when the browser is using JScript or JavaScript (friggin' Microsoft :-D) and how the two may respond differently.
I have been reading a lot on the global issues, but none of the suggested fixes has worked for me. One thing that I've learned is that in IE7, all three of these methods of initiating an external javascript file work: eval(), execScript(), and setTimeout().
Here is the code for your viewing pleasure (I break my code out for ease of reading; so I apologize if it seems longer than normal):
[code=javascript]
<!--
var xmlhttp;
var fileType;
function loadData(url)
{
xmlhttp = null;
fileType = url;
// code for native xmlhttp
if (window.XMLHttp Request)
{
xmlhttp = new XMLHttpRequest( );
}
// code for old IE
else if (window.ActiveX Object)
{
xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
}
if (xmlhttp != null)
{
xmlhttp.open("G ET", url, false);
xmlhttp.onready statechange = processData;
xmlhttp.send(nu ll);
}
else
{
alert("Please upgrade to a browser that supports XMLHTTP.");
}
}
function processData()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyS tate == 4)
{
// if "OK"
if (xmlhttp.status == 200)
{
// if text document
if (fileType.lastI ndexOf("txt") != -1)
{
document.getEle mentById('conte nt').innerHTML = xmlhttp.respons eText;
}
// if xml document
if (fileType.lastI ndexOf("xml") != -1)
{
// xml parse code here for xmlhttp.respons eXML
}
// if javascript file
if (fileType.lastI ndexOf("js") != -1)
{
// if MSIE
if (window.execScr ipt)
{
window.execScri pt(xmlhttp.resp onseText, "javascript ");
return null;
}
// if others
else if (window.eval)
{
window.eval(xml http.responseTe xt);
}
// if Safari
else
{
window.setTimeo ut(xmlhttp.resp onseText, 0);
}
}
}
else
{
alert("Problem retrieving data:\n" + xmlhttp.status + "\n" + xmlhttp.statusT ext);
}
}
}
//-->
[/code]
Thanks in advance for your help.
Comment