From 6982c14dd29963c8cf99706d451ba8d58ccff2a6 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Thu, 28 Feb 2013 18:36:05 +0100 Subject: [PATCH] check for response content * if it's empty don't process it and print a message into html * if there is useful data, process is and print the data as table into html --- scripts/haltestellen.js | 97 ++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/scripts/haltestellen.js b/scripts/haltestellen.js index 80d5e14..234ab10 100644 --- a/scripts/haltestellen.js +++ b/scripts/haltestellen.js @@ -9,6 +9,7 @@ function getHTTPObject() { // debug log if (DEBUG === 1) {console.log("xml http object function");} + // variable definitions var xhr; // check for availibility if xmlhttprequest object @@ -29,8 +30,9 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { // debug log if (DEBUG === 1) {console.log("ajax function");} - // get the xmlhttp object which is supported - var request = getHTTPObject(); + // variable definitions + var response; + var request = getHTTPObject(); // get the xmlhttp object which is supported outputElement.innerHTML = "Lade Daten ..."; @@ -40,11 +42,11 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { //save ajax response if (responseType === "json") { - var response = JSON.parse(request.responseText); + response = JSON.parse(request.responseText); } else if (responseType === "xml") { - var response = request.responseXML; + response = request.responseXML; } else { - var response = request.responseText; + response = request.responseText; } // check if callback is a function @@ -63,15 +65,18 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { // wrap all in anonymous function to get out of global scope (function() { + //variable definitions + var serverUrl; + var origin = window.location.origin.indexOf("www.goodcleanfun.de"); + // debug log if (DEBUG === 1) {console.log("anonymous function");} // server url - var origin = window.location.origin.indexOf("www.goodcleanfun.de"); if (origin === -1) { - var serverUrl = "http://goodcleanfun.de/cgi-bin/"; + serverUrl = "http://goodcleanfun.de/cgi-bin/"; } else { - var serverUrl = "http://www.goodcleanfun.de/cgi-bin/"; + serverUrl = "http://www.goodcleanfun.de/cgi-bin/"; } // get the search form @@ -94,51 +99,63 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { var hstName = document.getElementById("q").value; var hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?ort=dresden&hst=" + hstName); + // get the data from the server with an ajax call ajaxCall(hstUrl, target, function(data) { // debug log if (DEBUG === 1) {console.log("received data: " + data);} - data = data.replace(/\],\[/gi, '#'); - data = data.replace(/\(.+?\)/gi, ''); - data = data.replace('ß', 'ss'); - data = data.replace(/<(.+?)>/gi, '$1'); - data = data.slice(3,-3).split("#"); - - // debug log - if (DEBUG === 1) {console.log("parsed data: " + data);} - + //variable definitions var i; var y; var htmlOutput; - var dataLength = data.length; - - // generate table header - htmlOutput = ""; - htmlOutput += ""; - htmlOutput += ""; - htmlOutput += ""; - htmlOutput += ""; - htmlOutput += ""; - - // generate table entries - for (i = 0; i < dataLength; i++) { - htmlOutput += ""; - var entry = data[i].split(","); + var entry; + var dataLength; + + // process of response only if it's not empty + if (data.indexOf("[]") === -1) { + + // replace useless chars + // split string into array + data = data.replace(/\],\[/gi, '#'); + data = data.replace(/\(.+?\)/gi, ''); + data = data.replace('ß', 'ss'); + data = data.replace(/<(.+?)>/gi, '$1'); + data = data.slice(3,-3).split("#"); + // debug log - if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);} - for (y = 0; y < 3; y++) { + if (DEBUG === 1) {console.log("parsed data: " + data);} + + dataLength = data.length; + + // generate table header + htmlOutput = "
LinieRichtungAbfahrt
"; + htmlOutput += ""; + htmlOutput += ""; + htmlOutput += ""; + htmlOutput += ""; + htmlOutput += ""; + + // generate table entries + for (i = 0; i < dataLength; i++) { + htmlOutput += ""; + entry = data[i].split(","); // debug log - if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);} - htmlOutput += ""; + if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);} + for (y = 0; y < 3; y++) { + // debug log + if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);} + htmlOutput += ""; + } + htmlOutput += ""; } - htmlOutput += ""; + // close table + htmlOutput += "
LinieRichtungAbfahrt
" + entry[y].slice(1,-1) + "" + entry[y].slice(1,-1) + "
"; + } else { // there was an empty response + htmlOutput = "

Haltestelleneingabe nicht eindeutig

"; } - // close table - htmlOutput += ""; - - // print table into web page + // print content into web page target.innerHTML = htmlOutput; }, "text"); }