Loading XML config file for web page

T

TMN

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
 
M

Martin Honnen

TMN said:
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

Yes, use XMLHttpRequest for best cross-browser support to load the file.
or is there a way of including it in the <head> ?

Only IE has a concept of a so called XML data island where you can load
XML data inside of a HTML document using an 'xml' element e.g.
<xml id="x1" src="file.xml"></xml>
But that is not supported by other browsers.
 
T

The Magpie

TMN said:
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> ?
Use XMLHttpRequest to collect the data.
 
T

TMN

Use XMLHttpRequest to collect the data.

Thanks - It just seems something like <xml id="x1" src="file.xml"> </
xml> is so natural for a browser !!!

regards
Tim
 
J

joe

TMN said:
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("http://www.yourhomepage.com/somefile.txt");



function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=whatever;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support it.");
}
}

function whatever()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
myvar=1; //remove this line, not necessary
alert("It worked, here's the contents:" + xmlhttp.responseText);
}
else if (xmlhttp.status==404)
{
alert("Dang! no workee:" + xmlhttp.responseText);
}
else
{
alert("Som uther problemo:" + xmlhttp.responseText);
}
}
}
 
T

Tim Nash (aka TMN)

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("http://www.yourhomepage.com/somefile.txt");

function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=whatever;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support it.");
}

}

function whatever()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
myvar=1; //remove this line, not necessary
alert("It worked, here's the contents:" + xmlhttp.responseText);
}
else if (xmlhttp.status==404)
{
alert("Dang! no workee:" + xmlhttp.responseText);
}
else
{
alert("Som uther problemo:" + xmlhttp.responseText);
}
}

}

Thanks to all who responded.

Tim
South Africa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top