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.

108 lines
2.8 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 = eval(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. var wurst = [["13","Prohlis","754"],["13","Prohlis","754"],["13","Prohlis","754"],["13","Prohlis","754"]];
  37. console.log(wurst[0][0]);
  38. // get the search form
  39. var searchForm = document.getElementById("search-form");
  40. // haltestelle object
  41. var haltestelle = {
  42. getInfo : function(event) {
  43. console.log("getInfo function");
  44. // prevent submit default behaviour
  45. event.preventDefault();
  46. // get output area
  47. var target = document.getElementById("output");
  48. var hstName = document.getElementById("q").value;
  49. var hstUrl = serverUrl + "Abfahrten.do?ort=dresden&hst=" + hstName;
  50. ajaxCall(hstUrl, target, function(data) {
  51. var i;
  52. var y;
  53. var dataLength = data.length;
  54. target.innerHTML = "<table>";
  55. target.innerHTML += "<tr>";
  56. target.innerHTML += "<th>Linie</th>";
  57. target.innerHTML += "<th>Richtung</th>";
  58. target.innerHTML += "<th>Abfahrt</th>";
  59. target.innerHTML += "</tr>";
  60. for (i = 0; i < dataLength; i++) {
  61. target.innerHTML += "<tr>";
  62. for (y = 0; y < 3; y++) {
  63. target.innerHTML += "<td>" + data[i][y] + "</td>";
  64. }
  65. target.innerHTML += "</tr>";
  66. }
  67. target.innerHTML += "</table>";
  68. });
  69. }
  70. };
  71. //event listeners
  72. searchForm.addEventListener("submit", haltestelle.getInfo, false);
  73. })(); // end of anonymous function