Почему временное перенаправление JavaScript не работает? - PullRequest
0 голосов
/ 02 июня 2018

Следующее перенаправление JavaScript не работает - страница просто продолжает запрашивать целевой URL, а таймер просто уходит в минус бесконечность.(Обратите внимание, что я использую откат мета-обновления для клиентов без поддержки javascript.)

 setInterval(function() {
     var span = document.querySelector("#counter");
     var count = span.textContent * 1 - 1;
     span.textContent = count;
     if (count <= 0) {
         window.location.replace("https://www.siliconharvest.com.au");
     }
 }, 1000);
<!DOCTYPE html>
<html>
   <head>
      <noscript>
         <meta http-equiv="refresh" content="500" />
      </noscript>
   </head>
   <body>
      <!-- template from https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_redirect
         redirect guide https://davidwalsh.name/meta-refresh-javascript
         htaccess re-write: https://stackoverflow.com/questions/20154805/rewrite-all-urls-to-http-domain-com
         countdown redirect: https://stackoverflow.com/questions/3292038/redirect-website-after-certain-amount-of-time?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
         -->
      <p>We've been re-branded as "Silicon Harvest" and have moved to a new address!  The new URL is: <a href="https://www.siliconharvest.com.au">https://www.siliconharvest.com.au</a></p>
      <p>You will be redirected to the new address in <span id="counter" style="font-size:150%">5</span> seconds.</p>
      <p>If you are not redirected after 5 seconds, please click on the link above!</p>
     
   </body>
</html>

1 Ответ

0 голосов
/ 02 июня 2018

Попробуйте остановить интервал перед перенаправлением:

const timer = setInterval(function() {
     var span = document.querySelector("#counter");
     var count = span.textContent * 1 - 1;
     span.textContent = count;
     if (count <= 0) {
         clearInterval(timer);
         window.location.replace("https://www.siliconharvest.com.au");
     }
 }, 1000);
<!DOCTYPE html>
<html>
   <head>
      <noscript>
         <meta http-equiv="refresh" content="500" />
      </noscript>
   </head>
   <body>
      <!-- template from https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_redirect
         redirect guide https://davidwalsh.name/meta-refresh-javascript
         htaccess re-write: https://stackoverflow.com/questions/20154805/rewrite-all-urls-to-http-domain-com
         countdown redirect: https://stackoverflow.com/questions/3292038/redirect-website-after-certain-amount-of-time?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
         -->
      <p>We've been re-branded as "Silicon Harvest" and have moved to a new address!  The new URL is: <a href="https://www.siliconharvest.com.au">https://www.siliconharvest.com.au</a></p>
      <p>You will be redirected to the new address in <span id="counter" style="font-size:150%">5</span> seconds.</p>
      <p>If you are not redirected after 5 seconds, please click on the link above!</p>
     
   </body>
</html>
...