html - How to redirect a webpage using an array of urls in Javascript? -
what if have array urls want webpage redirect to, how can loop array forever, unless close browser?
let’s have
var urls = ['/news/1', 'news/2', 'news/3', '/update/1', '/news4', 'news/5', 'news/6', 'update/2'] etc….
i have single html template changes content of elements inside based database records. want redirect page (same html template) following urls every 10 minutes.
i have:
<script> var timer = settimeout(function() { window.location=‘/news/1' }, 10000); // redirect once news/1 after 10seconds. </script>
how can proper loop here on url array?
thank reading!
you can check on url are, update index, , start timeout. put in every page:
var urls = ['/news/1', '/news/2', '/news/3', '/update/1', '/news4', '/news/5', '/news/6', '/update/2'], currentpath = window.location.pathname, currentindex = urls.findindex(function(url){ return currentpath == url; }); if(currentindex < urls.length - 1){ currentindex++; } else { currentindex = 0; } settimeout(function(){ window.location.pathname = urls[currentindex]; }, 10000);
note: here, used
window.location.pathname
, part of url right after domain name. work, need have absolute paths slash @ beginning. use full urls, in case, usewindow.location.href
instead ofwindow.location.pathname
.
Comments
Post a Comment