TMN <na******@gmail .comwrote:
>Hi All
If I want to use an xml file to hold some config parameters for a web
page do I need to load it with JS
or is there a way of including it in the <head?
Thanks
Tim
Here a sample how to load a page with Javascript.
Be adivsed that it returns immediately. So if you use it to intialize variables
do them in "whatever" function.
Here' what I mean (pseudocode):
line 0: ..
line 1: blah, blah;
line 2: loadXMLDoc(..); //loads a value to "myvar"
line 3: if (myvar==1)
line 4: blah, blah;
line 5: ...
When execution of code reaches Line 4 you'd expect "myvar" to have some value.
Not so! Caveat! "whatever" is executed (see below example code) when it's good
and ready. loadXMLDoc(..) returns immediately (well almost).
loadXMLDoc("htt p://www.yourhomepag e.com/somefile.txt");
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttp Request)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest( );
}
else if (window.ActiveX Object)
{// code for IE6, IE5
xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
}
if (xmlhttp!=null)
{
xmlhttp.onready statechange=wha tever;
xmlhttp.open("G ET",url,true) ;
xmlhttp.send(nu ll);
}
else
{
alert("Your browser does not support it.");
}
}
function whatever()
{
if (xmlhttp.readyS tate==4)
{// 4 = "loaded"
if (xmlhttp.status ==200)
{// 200 = "OK"
myvar=1; //remove this line, not necessary
alert("It worked, here's the contents:" + xmlhttp.respons eText);
}
else if (xmlhttp.status ==404)
{
alert("Dang! no workee:" + xmlhttp.respons eText);
}
else
{
alert("Som uther problemo:" + xmlhttp.respons eText);
}
}
}