admin管理员组

文章数量:1406942

i have countdown timer with javascript, I want to redirect to another page after countdown is over.countdown works pefectly but it's not redirecting to the result page

i tried this:

app.js

var count = 15

var counter = setInterval(timer , 1000)

function timer(){
   count = count-1
   if (count <= 0)
   {
       clearInterval(counter);
       return window.location.replace("{% url'app:result' %}")
   }
   document.getElementById("timer").innerHTML= count + " secs";

}

i have countdown timer with javascript, I want to redirect to another page after countdown is over.countdown works pefectly but it's not redirecting to the result page

i tried this:

app.js

var count = 15

var counter = setInterval(timer , 1000)

function timer(){
   count = count-1
   if (count <= 0)
   {
       clearInterval(counter);
       return window.location.replace("{% url'app:result' %}")
   }
   document.getElementById("timer").innerHTML= count + " secs";

}
Share Improve this question asked May 21, 2020 at 8:07 Swapnil MangaonkerSwapnil Mangaonker 611 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can use this:

 window.location.href = "{% url'app:result' %}"

django template tags work inside the django templates. Since you have above javascript in the app.js file, this {% url'app:result' %} tag in the line below won't work because it is not valid javascript.

return window.location.replace("{% url'app:result' %}")

You can try moving the code from app.js into the corresponding django template and see if that works

本文标签: Redirect django url with javascriptStack Overflow