From: foka on
Hi.I have got a problem with javascript and XML.
I want to put data from XML file to HTML <select> options.
My code is working but only if I put this (websites) on server, but I
want to run my html site on every comp, localy, for example from CD., on
windows without Apache etc.
When I run my site localy file:///C:/file.html then my <select> is empty.

My Ajax code:
function showGroups()
{
var url_file="../xml/groups.xml";
xmlDoc=loadXMLDoc(url_file); //wczytuje dane
var groupXml = xmlDoc.getElementsByTagName("group");
for (var i=0; i < groupXml.length; i++) {
document.forms['searchForm'].grupy.options[i] = new
Option(groupXml[i].firstChild.nodeValue,groupXml[i].firstChild.nodeValue);
}
}
window.onload=showGroups;

HTML part:

<select name="grupy" class="grupy">
</select>

My XML file
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<group>AK</group>
<name>COS AK</name>
</item>
<item>
<group>BZ</group>
<name>COS BZ</name>
</item>
<item>
<group>CA</group>
<name>COS CA</name>
</item>
</items>

Is there anyone who can help me?

Seal
From: Stefan Weiss on
On 22/04/10 17:56, foka wrote:
> Hi.I have got a problem with javascript and XML.
> I want to put data from XML file to HTML <select> options.
> My code is working but only if I put this (websites) on server, but I
> want to run my html site on every comp, localy, for example from CD., on
> windows without Apache etc.
> When I run my site localy file:///C:/file.html then my <select> is empty.
>
> My Ajax code:
> function showGroups()
> {
> var url_file="../xml/groups.xml";
> xmlDoc=loadXMLDoc(url_file); //wczytuje dane
[snip]

I assume that you've declared the xmlDoc variable somewhere else.

Which browser are you using, and what exactly does loadXMLDoc do (except
that it "reads data", if I translated the comment correctly)?

> var groupXml = xmlDoc.getElementsByTagName("group");

Did this line trigger an error? If not, did you enable error reporting
in your browser? If there really is no error, how come you get a valid
XML document that is somehow missing its <group> tags?

By the way, you may want use an asynchronous XMLHttpRequest.


--
stefan
From: VK on
On Apr 22, 7:56 pm, foka <crazys...(a)onet.eu> wrote:
> Hi.I have got a problem with javascript and XML.
> I want to put data from XML file to HTML <select> options.
> My code is working but only if I put this (websites) on server, but I
> want to run my html site on every comp, localy, for example from CD., on
> windows without Apache etc.
> When I run my site localy file:///C:/file.html then my <select> is empty.

Your loadXMLDoc (whatever it is) expects response status "200 OK" to
proceed, but when reading from the local file system the response
status is 0. Wherever it is, change
if (yourRequest.status == 200) { ...
to
if ((yourRequest.status == 200) || (yourRequest.status == 0)) { ....

P.S. Unless your currently security settings simply prohibit script
execution for local pages.
From: foka on
My javascript function:

function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

What u suggest to correct? What and where to add?


VK pisze:
> On Apr 22, 7:56 pm, foka <crazys...(a)onet.eu> wrote:
>> Hi.I have got a problem with javascript and XML.
>> I want to put data from XML file to HTML <select> options.
>> My code is working but only if I put this (websites) on server, but I
>> want to run my html site on every comp, localy, for example from CD., on
>> windows without Apache etc.
>> When I run my site localy file:///C:/file.html then my <select> is empty.
>
> Your loadXMLDoc (whatever it is) expects response status "200 OK" to
> proceed, but when reading from the local file system the response
> status is 0. Wherever it is, change
> if (yourRequest.status == 200) { ...
> to
> if ((yourRequest.status == 200) || (yourRequest.status == 0)) { ...
>
> P.S. Unless your currently security settings simply prohibit script
> execution for local pages.


--
Pozdrawiam
Mariusz Bartyzel

---------------------
www.reservoirdogs.pl
From: foka on
> I assume that you've declared the xmlDoc variable somewhere else.
>
> Which browser are you using, and what exactly does loadXMLDoc do (except
> that it "reads data", if I translated the comment correctly)?
>
I used every browser. IE8, Chrome, Firefox.
Everything is ok if I run my site on serwer( I have wamp server on
Windows to test)


>
> Did this line trigger an error? If not, did you enable error reporting
> in your browser? If there really is no error, how come you get a valid
> XML document that is somehow missing its <group> tags?
>
> By the way, you may want use an asynchronous XMLHttpRequest.
>

I'm beginner. There is no errors. XML file is correct.

My whole test.html site:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/>
<title>Techrol - Części zamienne do ciągników i maszyn</title>
<link rel="stylesheet" type="text/css" href="css/techrol.css"
media="screen"/>
<script type="text/javascript" src="../js/functions.js"></script>
<script type="text/javascript" language="javascript">

function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}


function showGroups()
{
var url_file="../xml/roups.xml";
xmlDoc=loadXMLDoc(url_file);

var groupXml = xmlDoc.getElementsByTagName("group");
for (var i=0; i < groupXml.length; i++) {
document.forms['searchForm'].groups.options[i] =
new
Option(groupXml[i].firstChild.nodeValue,groupXml[i].firstChild.nodeValue);
}
}

window.onload=showGroups;

</script>

</head>
<body>
<div id="header">
<img alt="" src="images/logo.jpg" /><h1>Header</h1>
</div>
<div id="content">
<div id="search">

<form id="searchForm"
action="files/techrol_search.html" method="get" onSubmit="return
checkSearch()">
<div id="searchField"><input name="search"
class="input" type="text" value=""/></div>
&nbsp;<span>Grupa:</span>
<select name="groups" class="groups">
</select>
<input class="submit" type="submit"
value="Szukaj" />
</div>
</form>
</div>

</div>
</body>
</html>
---------------------------------------




foka
 |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9
Prev: Quicktime and innerHTML
Next: option & textnode