I'm currently trying to follow an example I found in a book a while ago to try to create a spotlight (mac-like) search that filters results as one types. The script that I have works perfectly in Firefox, but does not seem to work successfully in Internet Explorer (I'm using version 7).
Here is the Javascript code I am currently using:
Expand|Select|Wrap|Line Numbers
- window.onload = initAll;
- var xhr = false;
- var statesArray = new Array();
- function initAll() {
- document.getElementById("searchField").onkeyup = searchSuggest;
- xhr = null;
- // code for Mozilla, etc.
- if (window.XMLHttpRequest) {
- xhr = new XMLHttpRequest();
- }
- // code for IE
- else if (window.ActiveXObject) {
- xhr = new ActiveXObject("Microsoft.XMLHTTP");
- }
- if (xhr != null){
- xhr.onreadystatechange=setStatesArray;
- xhr.open("GET","search.xml",true);
- xhr.send(null);
- } else {
- alert("Your browser does not support XMLHTTP.");
- }
- }
- function setStatesArray() {
- if(xhr.readyState == 4) {
- if(xhr.responseXML) {
- var allStates = xhr.responseXML.getElementsByTagName("item");
- for(var i = 0; i < allStates.length; i++){
- statesArray[i] = allStates[i].getElementsByTagName("label")[0].firstChild;
- } // for
- } // if
- else {
- alert("There was a problem with the request " + xhr.status);
- } // else
- } // if
- } // setStatesArray
- function searchSuggest(){
- var str = document.getElementById("searchField").value;
- document.getElementById("searchField").className = "";
- if(str != "") {
- document.getElementById("popups").innerHTML = "";
- for(var i = 0; i < statesArray.length; i++) {
- var thisState = statesArray[i].nodeValue;
- if(thisState.toLowerCase().indexOf(str.toLowerCase()) == 0){
- var tempDiv = document.createElement("div");
- tempDiv.innerHTML = thisState;
- tempDiv.onclick = makeChoice;
- tempDiv.className = "suggestions";
- document.getElementById("popups").appendChild(tempDiv);
- } // if
- } // for
- var foundCt = document.getElementById("popups").childNodes.length;
- if(foundCt == 0) {
- document.getElementById("searchField").className = "error";
- } // if
- } // if
- else {
- document.getElementById("popups").innerHTML = "";
- } // else
- } // searchSuggest
- function makeChoice(evt){
- var thisDiv = (evt) ? evt.target :window.event.srcElement;
- document.getElementById("searchField").value = thisDiv.innerHTML;
- document.getElementById("popups").innerHTML = "";
- //Test Redirection
- if (document.getElementById("searchField").value == "Google") {
- window.location = "http://www.google.com/";
- } // if
- } //makeChoice
As you can probably tell, this script is used to access a XML document containing values. Then, as a user types their search query into a text box, the script will search through the list of XML values and display the ones that match the user's query. If, however, no matches are found, the className is changed to "error" (causing the input field to turn a shade of yellow).
When I enter any value into the search field in Internet Explorer, even if it is one that should come up with a result, the search field instantly turns yellow, indicating that no matches could be found. I believe that this problem stems from the fact that the array "statesArray" has no value as when I open the javascript error console in IE, I get an error saying:
Expand|Select|Wrap|Line Numbers
- var thisState = statesArray[i].nodeValue; is null or not an object.
If anyone can see what I'm doing wrong, information would be greatly appreciated.
Thanks,
MrWelfare