Toon Van Dooren
Toon Van Dooren

Reputation: 613

search function javascript submit

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();

Upvotes: 0

Views: 3510

Answers (2)

mplungjan
mplungjan

Reputation: 177683

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:

<html>
<head>
<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;
}
var xmlDoc=loadXMLDoc("data.xml");
var nodeList = xmlDoc.getElementsByTagName("article");
function search(theForm) {
  var name = theForm.myname.value.toLowerCase();
  var html = "";
  for (var i = 0; i < nodeList.length; i++) {
    var titleNode = nodeList[i];
    if (titleNode.getElementsByTagName("title")[0].childNodes[0].nodeValue.toLowerCase() == name) {
      html += "<div style='width:450px;'>";
      html += "<p>"+titleNode.getElementsByTagName("title")[0].childNodes[0].nodeValue+"</p>";
      html += "<p>"+titleNode.getElementsByTagName("description")[0].childNodes[0].nodeValue+"</p>";
      html += "<p>"+titleNode.getElementsByTagName("urltext")[0].childNodes[0].nodeValue+"</p>";
      html+= "</div>";
      document.getElementById("result").innerHTML=html
    }
  }
  return false; // cancel the submit
}
</script>
</head>
<body>
<form id="oForm" name="oForm" onsubmit="return search(this)">
<input type="text" name="myname" id="txt_name" size="30" maxlength="70">
<input type="submit" value="klik"/>
</form>
<div id="result"></div>
</body>
</html>

Upvotes: 2

Nitzan Tomer
Nitzan Tomer

Reputation: 164129

try this:

<input type="submit" value="klik" onclick="return search()"/>

and in the search method:

function search() {
   .....

   return false;
}

EDIT 1: you can also add an event for the form itself:

<form onsubmit="return false">

(http://www.w3schools.com/jsref/event_form_onsubmit.asp)

EDIT 2: This works for me:

<form onsubmit="return false">
    <input type="submit" value="Go" onclick="alert('yo');"/>
</form>

Upvotes: -1

Related Questions