Home > Enterprise >  using vanilla js how do i make the url update
using vanilla js how do i make the url update

Time:12-18

how do i make the window.location.url update every 30 seconds to the next id found in the array. Right now i have it alerting hello every few seconds but want the url to change instead

    const myIds=['1_aq4jiqb','1_4u0ocu4u'];

for (let i = 0; i < myIds.length; i  ) {
window.location.href = 'https://yahoo.com'   myIds[i]  '/embed/iframe?';

}
setInterval(function(){ alert("Hello"); }, 3000);
<html>


</html>

update to the next Id after 30 seconds?

CodePudding user response:

It's not possible unless you control each of those sites that you're redirecting to.

When you do window.location.href =, an entirely new document is loaded, with (and with only) the JavaScript(s) that new document links to. Anything that happens in your code after a single window.location.href = runs will be effectively ignored, because your old JavaScript does not carry over to the newly-loaded page.

Unless you control each of the sites you're redirecting to, the only way to accomplish this (on your browser only) would be to inject JavaScript code of your own into every site with a userscript.

CodePudding user response:

If I understand what your're asking, just put the code inside a function, and call that at the setInterval.

const myIds=['1_aq4jiqb','1_4u0ocu4u'];

function switchId() {
  for (let i = 0; i < myIds.length; i  ) {
    window.location.href = 'https://yahoo.com'   myIds[i]  '/embed/iframe?';
  }
}

setInterval(function(){ switchId() }, 3000);
<html>


</html>

  • Related