Some experiments with web applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB

  1. // server url
  2. var serverUrl = "http://widgets.vvo-online.de/abfahrtsmonitor/";
  3. // xmlhttp object function
  4. function getHTTPObject() {
  5. console.log("xml http object function");
  6. var xhr;
  7. if(window.XMLHttpRequest) {
  8. xhr = new XMLHttpRequest();
  9. } else if(window.ActiveXObject) {
  10. xhr = new ActiveXObject("Msxml2.XMLHTTP");
  11. }
  12. return xhr;
  13. }
  14. // ajax call function
  15. function ajaxCall(dataUrl, outputElement, callback) {
  16. console.log("ajax function");
  17. // get the xmlhttp object which is supported
  18. var request = getHTTPObject();
  19. outputElement.innerHTML = "Lade Daten ...";
  20. request.onreadystatechange = function() {
  21. if(request.readyState === 4 && request.status === 200) {
  22. //save ajax response
  23. var response = request.responseText;
  24. // check if callback is a function
  25. if(typeof callback === "function") {
  26. callback(response);
  27. }
  28. }
  29. };
  30. request.open("get", dataUrl, true);
  31. request.send(null);
  32. }
  33. // wrap all in anonymous function to get out of global scope
  34. (function() {
  35. console.log("anonymous function");
  36. // get the search form
  37. var searchForm = document.getElementById("search-form");
  38. // haltestelle object
  39. var haltestelle = {
  40. getInfo : function(event) {
  41. console.log("getInfo function");
  42. // prevent submit default behaviour
  43. event.preventDefault();
  44. // get output area
  45. var target = document.getElementById("output");
  46. var hstName = document.getElementById("q").value;
  47. var hstUrl = serverUrl + "Abfahrten.do?ort=dresden&hst=" + hstName;
  48. ajaxCall(hstUrl, target, function(data) {
  49. console.log("received data: " + data);
  50. data = data.replace(/\],\[/gi, '#');
  51. data = data.replace(/\(.+\),\[/gi, '#');
  52. data = data.slice(2,-2).split("#");
  53. console.log("parsed data: " + data);
  54. var i;
  55. var y;
  56. var htmlOutput;
  57. var dataLength = data.length;
  58. // generate table header
  59. htmlOutput = "<table>";
  60. htmlOutput += "<tr>";
  61. htmlOutput += "<th>Linie</th>";
  62. htmlOutput += "<th>Richtung</th>";
  63. htmlOutput += "<th>Abfahrt</th>";
  64. htmlOutput += "</tr>";
  65. // generate table entries
  66. for (i = 0; i < dataLength; i++) {
  67. htmlOutput += "<tr>";
  68. data = data[i].split(",");
  69. console.log("part " + i + " of parsed data: " + data);
  70. for (y = 0; y < 3; y++) {
  71. console.log("part " + y + ": " + data[y]);
  72. htmlOutput += "<td>" + data[y].slice(1,-1) + "</td>";
  73. }
  74. htmlOutput += "</tr>";
  75. }
  76. // close table
  77. htmlOutput += "</table>";
  78. // print table into web page
  79. target.innerHTML = htmlOutput;
  80. });
  81. }
  82. };
  83. //event listeners
  84. searchForm.addEventListener("submit", haltestelle.getInfo, false);
  85. })(); // end of anonymous function