Connecting Tech Pros Worldwide Forums | Help | Site Map

Loading XML config file for web page

TMN
Guest
 
Posts: n/a
#1: Jun 27 '08
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

Martin Honnen
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Loading XML config file for web page


TMN wrote:
Quote:
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.
Quote:
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.


--

Martin Honnen
http://JavaScript.FAQTs.com/
The Magpie
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Loading XML config file for web page


TMN wrote:
Quote:
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.
TMN
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Loading XML config file for web page


On May 14, 6:33 pm, The Magpie <use...@pigsinspace.co.ukwrote:
Quote:
TMN wrote:
Quote:
Hi All
>
Quote:
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.
Thanks - It just seems something like <xml id="x1" src="file.xml"</
xmlis so natural for a browser !!!

regards
Tim
joe
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Loading XML config file for web page


TMN <nash.rsa@gmail.comwrote:
Quote:
>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);
}
}
}


Tim Nash (aka TMN)
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Loading XML config file for web page


On May 15, 7:53 am, joe <m...@invalid.comwrote:
Quote:
TMN <nash....@gmail.comwrote:
Quote:
Hi All
>
Quote:
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?
>
Quote:
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);
}
}
>
}
Thanks to all who responded.

Tim
South Africa
Closed Thread