So i am trying to make a function that searches trough an xml file, my results are given but the page is refreshed and keeps loading. I figured this was because of a submit but i used return false and i still have the same problem. Can anyone help me?
Below is my code
<script type="text/javascript">
function loadXMLDoc(XMLname)
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",XMLname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(XMLname);
return xmlDoc;
}
alert("Error loading document!");
return null;
}
</script>
</head>
<body>
<form id="oForm" name="oForm" onsubmit="search(); return false">
<input type="text" name="name" id="txt_name" size="30" maxlength="70">
<input type="submit" value="klik" onclick="search()"/>
</form>
<script type="text/javascript">
function search(){
name = document.oForm.name.value.toLowerCase();
xmlDoc=loadXMLDoc("data.xml");
var nodeList = xmlDoc.getElementsByTagName("article");
for (var i = 0; i < nodeList.length; i++) {
var titleNode = nodeList[i];
if(titleNode.getElementsByTagName("title")[0].childNodes[0].nodeValue.toLowerCase() == name){
document.write("<div style='width:450px;'>")
document.write("<p>"+titleNode.getElementsByTagName("title")[0].childNodes[0].nodeValue+"</p>");
document.write("<p>"+titleNode.getElementsByTagName("description")[0].childNodes[0].nodeValue+"</p>");
document.write("<p>"+titleNode.getElementsByTagName("urltext")[0].childNodes[0].nodeValue+"</p>");
document.write("</div>")
}
}
}
</script>
I would like to repeat that i do get results i only lose my layout and the page keeps loading. I made this for safari/safari mobile so i would apreciate it if someone could present a solution. I cant use any serversided scripts either since i need to be able to cache this offline so i figure it has to be javascript.
Ty in advance
EDIT
<input type="text" name="name" id="txt_name" size="30" maxlength="70">
<input type="button" value="klik" onclick="search();"/>
name = GetElementById("txt_name").value.toLowerCase();
Assuming the xml processing code works, do it like this – document.write WIPES the page
PS: Firefox MAY not work as expected due to linefeeds in the XML: