Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Przekierowanie z licznikiem...
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
przemasisko
Witam! Znalazłem gdzieś taki oto skrypt, i chciałbym go troszkę przerobić.
Tzn. żeby przekierowanie nastąpiło po 20 sek. a nie 30 sek., oraz jak przekierować na inną domenę, bo

  1. <?php
  2. makeRequest("http://testowa-domena.pl");
  3. ?>


nie działa w tym skrypcie poniżej... worriedsmiley.gif

  1. <?php
  2. <html>
  3.  <head>
  4.  <title>www.test.pl</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2" >
  6.  
  7. <script language="javascript">
  8. var timerID = null;
  9. var timerRunning = false;
  10. var timer_org = 30;var timerAktual = 30;var limit = true;var blokada = false;function stopclock()
  11. {
  12. if(timerRunning)
  13. clearTimeout(timerID)
  14. timerRunning = false;
  15. }
  16.  
  17. function startclock()
  18. {
  19. stopclock();
  20. showtime();
  21. }
  22.  
  23. function showtime()
  24. {
  25. if(!blokada)
  26. {
  27. document.clock.face.value = timerAktual;
  28. timerAktual--;
  29. if(timerAktual<0)
  30. {
  31. makeRequest("http://testowa-domena.pl");
  32. timerAktual=timer_org;
  33. }
  34.  
  35. timerID = setTimeout("showtime()",1000);
  36. timerRunning = true;
  37. }
  38. }
  39. var http_request = false;
  40. function makeRequest(url)
  41. {
  42. url = url + "?tstamp="+new Date();
  43. http_request = false;
  44. if (window.XMLHttpRequest)
  45. { // Mozilla, Safari,...
  46. http_request = new XMLHttpRequest();
  47. }
  48. else if (window.ActiveXObject)
  49. { // IE
  50. try
  51. {
  52. http_request = new ActiveXObject("Msxml2.XMLHTTP");
  53. }
  54. catch (e)
  55. {
  56. try
  57. {
  58. http_request = new ActiveXObject("Microsoft.XMLHTTP");
  59. }
  60. catch (e)
  61. {
  62. }
  63. }
  64. }
  65. if (!http_request)
  66. {
  67. alert('Błąd inicjalizacji');
  68. return false;
  69. }
  70. http_request.onreadystatechange = function() { alertContents(http_request); };
  71. http_request.open('GET', url, true);
  72. http_request.send(null);
  73.  
  74. }
  75.  
  76. function alertContents(http_request)
  77. {
  78. //alert(http_request.responseText);
  79. if (http_request.readyState == 4)
  80. {
  81.  if (http_request.status == 200)
  82. {
  83. if(http_request.responseText == '0')
  84. {
  85. window.location = "http://www.test.pl";
  86. }
  87. else
  88. {
  89. if(limit == true)
  90. {
  91. window.location = "http://www.test.pl";
  92. }
  93. else
  94. {
  95. //document.kolejka.numer.value = http_request.responseText;
  96. }
  97. }
  98.  }
  99. else if ((http_request.status == 408) || (http_request.status == 404) || (http_request.status == 503) || (http_request.status == 400))
  100. {
  101. window.location = "http://www.test.pl";
  102.  
  103. }
  104.  
  105.  
  106. }
  107. }
  108. //-->
  109. </script>
  110. <style>
  111. .infoBoxContents {
  112. background: #ffc000;
  113. font-family: Verdana;
  114. font-size: 10px;
  115. }
  116. .tabela {
  117.  background: #fffcd6;
  118.  font-family: Verdana;
  119.  font-size: 13px;
  120. }
  121.  </style>
  122.  </head>
  123.  
  124.  
  125. <form name="clock" onSubmit="0">
  126. <br>
  127. <font size = 4>Automatycznie strona odświeży się za
  128. <input type="text" name="face" size="1" style="text-align:center;border:0;background:rgb(255,252,214);font-size:18px">
  129. sekund.
  130. </font>
  131. </form>
  132. </font>
  133. <script>startclock();</script>
  134.  
  135.  </td>
  136. </tr>
  137.  </tbody>
  138. </table>
  139.  </body>
  140. </html>
  141. ?>
Cysiaczek
I gdzie problem? Zamien wartosci zmiennych z 30 na 20 i zmień adres, na który zostaniesz przekierowany.
Wszystko masz w tym kodzie napisane. Z tą różnicą, ze tam idzie httpRequest (AJAX), więc trzeba to wywalić.

To poniżej działa:
  1. <head>
  2. <title>www.test.pl</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2" >
  4.  
  5. <script language="javascript">
  6. var timerID = null;
  7. var timerRunning = false;
  8. var timer_org = 10;var timerAktual = 10;var limit = true;var blokada = false;function stopclock()
  9. {
  10. if(timerRunning)
  11. clearTimeout(timerID)
  12. timerRunning = false;
  13. }
  14.  
  15. function startclock()
  16. {
  17. stopclock();
  18. showtime();
  19. }
  20.  
  21. function showtime()
  22. {
  23. if(!blokada)
  24. {
  25. document.clock.face.value = timerAktual;
  26. timerAktual--;
  27. if(timerAktual<0)
  28. {
  29. makeRequest("http://testowa-domena.pl");
  30. timerAktual=timer_org;
  31. }
  32.  
  33. timerID = setTimeout("showtime()",1000);
  34. timerRunning = true;
  35. }
  36. }
  37. var http_request = false;
  38. function makeRequest(url)
  39. {
  40. window.location = url;
  41. }
  42.  
  43. //-->
  44. .infoBoxContents {
  45. background: #ffc000;
  46. font-family: Verdana;
  47. font-size: 10px;
  48. }
  49. .tabela {
  50. background: #fffcd6;
  51. font-family: Verdana;
  52. font-size: 13px;
  53. }
  54. </style>
  55. </head>
  56.  
  57.  
  58. <form name="clock" onSubmit="0">
  59. <br>
  60. <font size = 4>Automatycznie strona odświeży się za
  61. <input type="text" name="face" size="1" style="text-align:center;border:0;background:rgb(255,252,214);font-size:18px">
  62. sekund.
  63. </font>
  64. </form>
  65. </font>
  66. <script>startclock();</script>
  67.  
  68. </td>
  69. </tr>
  70. </tbody>
  71. </body>
  72. </html>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.