0

I am getting a list of URLs by gettig the href attribute from an array of elements that match a certain class name. Everything works great until here. Then I am trying to change the location.href of the page, every 10 seconds, by iterating on the array of URLs. For some reason, nothing happens though. What am I doing wrong?

var buttonsElements = document.getElementsByClassName("elementor-button-link elementor-button");
console.log(buttonsElements.length); 
for(i=0;i<buttonsElements.length;i++) {
    console.log(buttonsElements[i].href); 
    //everything works just fine until here

 setTimeout(function(){
        window.location.href = buttonsElements[i].href;
    },10000);
}
hajewa
  • 9
  • 1
  • https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example . Notice, that you need only a single URL, and this script has to be placed on every page. When the location has been changed, all the script in the previous page will be gone. – Teemu Jan 20 '20 at 13:26
  • Isn't really clear to me what you're trying to do. Note, once you assign the href the browser will bring you to another page and the script will be interrupted. Please tell us some other details and we could help you better. – Jazzpaths Jan 20 '20 at 13:32
  • If the script will be gone after the location has been changed, then I can't iterate on all the URLs, right? At this point maybe I should use another scripting or programming language. – hajewa Jan 20 '20 at 13:35
  • @JazzpathsWebStaff I basically wanted to iterate on all the URLs and fill automatically the form of each page. – hajewa Jan 20 '20 at 13:36
  • @hajewa, that's not the way to go then. Doing like you did, as told by me and others, will only bring you to another page and the script will be interrupted. Please provide the html of those pages. – Jazzpaths Jan 20 '20 at 13:39
  • 1
    Nowadays browsers know JavaScript only, there are no options for alternative scripting languages. Every page loaded into a browser creates a new document, and the previous document with its content and scripts is totally cleared, that's how the system works. Take a look at [iframe](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe), that's probably the easiest way to achieve what you need. If you'll find the iframe not being applicable for your task, you can search for "SPA" , and get familiar with the techniques of creating a Single Page Application. – Teemu Jan 21 '20 at 08:31

2 Answers2

1

Isn't really clear to me what you're trying to do. Note, once you assign the href the browser will bring you to another page and the script will be interrupted.

Jazzpaths
  • 645
  • 5
  • 9
-1

window.location.href = buttonsElements[i].href; i doesn't refer to anything in your function, is never incremented, etc... You iterated through the elements once using i in order to console.log() all the elements, but you're not touching i anywhere else in the code. You have to change its value somewhere.

Csmeau4
  • 30
  • 6
  • You can see the line `for(i=0;i – Teemu Jan 21 '20 at 07:32
  • As I said, he used the ```i++``` statement earlier in the code in order to ```console.log()``` everything, but that's the only place where ```i``` is used. (not where it matters) Plus, as pointed out by Jazzpaths Web Staff, his original idea doesn't make much sense because of how ```location.href``` works in the first place. – Csmeau4 Jan 21 '20 at 14:02